Hot File

Create C# Nested Try And Catch Blocks

View: 157    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: C / C++ / MFC   Fields: Other

10 point/1 review File has been tested

In C# one try block can contains another try block which is called nested try blocks.

Introduction

In C# one try block can contains another try block which is called nested try blocks.

Exception of an inner try block is handled by its inner catch block and exception of an outer try blockis handled by its outer catch block.

If an exception is occurred in an outer try block then the flow control transfer to an outer catch blockand inner exception or try block will be by passed

If an exception does not occurred in an outer try block but occurred in an inner try block then that is handled by an inner catch block

Remember the inner try blocks are associated with the inner catch blocks and outer are associated with the outer catch blocks

Example:

using System;
namespace csharpAdvance
{
    class ExceptionExample
    {
        // int type array.
        int[] Numbers = { 1, 2 };
 
        // void type method.
        public void Devision(int number, int devidedBy)
        {
            Console.WriteLine("Division " + number + "/" + devidedBy + " = " + (number / devidedBy));
        }
 
        // void type method GetArray().
        public void GetArray()
        {
            // for loop loops through each int number and assigns into an array.
            for (int count = 0; count < 4; count++)
            {
                Numbers[count] = count;
                Console.WriteLine(Numbers[count]);
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            // Object creation.
            ExceptionExample exceptionExample = new ExceptionExample();
 
            try
            { // An outer try block.
                exceptionExample.GetArray();
 
                try
                { // An inner/nested try block.
 
                     exceptionExample.Devision(10, 0);
 
                } // End of an inner try block.
 
                catch (DivideByZeroException)
                { // An inner catch block is associated with an inner try block.
 
                     Console.WriteLine("DivideByZeroException is caught");
 
                } // End of an inner catch block.
 
            } // End of an outer try block.
 
            catch (IndexOutOfRangeException)
            { // An outer catch block is associated with an outer try block.
 
                 Console.WriteLine("IndexOutOfRangeException is caught");
 
            } // End of an outer catch block.
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        0
        1
        IndexOutOfRangeException is caught
     */
}

Create C# Nested Try And Catch Blocks

Create C# Nested Try And Catch Blocks Posted on 08-09-2016  In C# one try block can contains another try block which is called nested try blocks. 10/10 157

Comment:

To comment you must be logged in members.

Files with category

  • How to Swap Two Numbers using Call by Reference in C

    View: 260    Download: 0   Comment: 0   Author: none  

    How to Swap Two Numbers using Call by Reference in C

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    View: 262    Download: 0   Comment: 0   Author: none  

    How to Print Fibonacci Series using Recursion in C, C++

    Category: C / C++ / MFC
    Fields: Other

    4.5/1 review
    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    View: 291    Download: 0   Comment: 0   Author: none  

    How to Implicitly Typed Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    View: 190    Download: 0   Comment: 0   Author: none  

    How to C# Static vs Instance Methods As Event Handlers

    Category: C / C++ / MFC
    Fields: Other

    0/0 review
    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    View: 339    Download: 0   Comment: 0   Author: none  

    Build Pass By Reference To Method In C#

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    View: 178    Download: 0   Comment: 0   Author: none  

    How to C# Continue Statement

    Category: C / C++ / MFC
    Fields: Other

    0/2 review
    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    View: 131    Download: 0   Comment: 0   Author: none  

    How to Single Dimensional Arrays In C#

    Category: C / C++ / MFC
    Fields: Other

    0/1 review
    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

  • Build C# Multilevel Inheritance

    View: 145    Download: 0   Comment: 0   Author: none  

    Build C# Multilevel Inheritance

    Category: C / C++ / MFC
    Fields: Other

    2.5/2 review
    C# supports multilevel inheritance, suppose we have four classes A, B, C and D. Class A can inherit to B, B to C and C can inherit to D and so on now class D has all public members of class A, B and C include its own.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd