C# Language Exception Handling Nesting of Exceptions & try catch blocks.

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

One is able to nest one exception / try catch block inside the other.

This way one can manage small blocks of code which are capable of working without disrupting your whole mechanism.

try 
{
//some code here
    try 
    {
        //some thing which throws an exception. For Eg : divide by 0
    }
    catch (DivideByZeroException dzEx)
    {
        //handle here only this exception
        //throw from here will be passed on to the parent catch block
    }
    finally
    {
        //any thing to do after it is done.
    }
 //resume from here & proceed as normal; 
}
catch(Exception e)
{
    //handle here
}

Note: Avoid Swallowing Exceptions when throwing to the parent catch block



Got any C# Language Question?