I faced a question on Custom Exceptions in an Interview. As below there are four multiple catches which catch each custom exception. Interviewer asked me to write a pseudo code for catch blocks how i can handle all four custom exceptions.
I answered as
try
{ try
{
try { }
catch (DEx dEx) { Console.WriteLine(dEx.Message); }
}
catch (BEx bEx) { Console.WriteLine(bEx.Message); }
catch (CEx cEx) { Console.WriteLine(cEx.Message); }
}
catch (AEx aEx) { Console.WriteLine(aEx.Message); }
Is it correct or wrong?
CEx and DEx inherits for BEx and BEx inherits from AEx. So exception hanlding should start from bottom to up. Base exception last.
try
{
// statements causing exception
}
catch( CEx cEx )
{
// error handling for CEx
}
catch( DEx dEx )
{
// error handling for DEx
}
catch( BEx bEx )
{
// error handling for BEx
}
catch( AEx aEx )
{
// error handling for AEx
}
finally
{
// statements to be executed
}