Exception Handling corrective approach
Hi,
below is just simple example as per my professional experience for exception handling
public class NestedExceptionHandling
{
public void MainMethod()
{
try
{
//some implementation
ChildMethod1();
}
catch (Exception exception)
{
//Handle exception
}
}
private void ChildMethod1()
{
try
{
//some implementation
ChildMethod2();
}
catch (Exception exception)
{
//Handle exception
throw;
}
}
private void ChildMethod2()
{
try
{
//some implementation
}
catch (Exception exception)
{
//Handle exception
throw;
}
}
}
So what would happen with the above code if the same exception were handled multiple times and moreover catching exceptions, throwing it will definitely add performance overhead.
Now observe this below code
public class NestedExceptionHandling
{
public void MainMethod()
{
try
{
//some implementation
ChildMethod1();
}
catch(Exception exception)
{
//Handle exception
}
}
private void ChildMethod1()
{
//some implementation
ChildMethod2();
}
private void ChildMethod2()
{
//some implementation
}
}
Comments
Post a Comment