Exception Handling
I have four methods m1(), m2(), m3(), m4(), whenever error occured in m4(), m3() should call, whenever error occured in m3(), m2() should call, whenever error occured in m2(), m1() should call.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pradeep ShetPosted Jan 19, 2015, 4:24 AM
try
{
m4();
}
catch
{
try
{
m3();
}
catch
{
try
{
m2();
}
catch
{
m1();
}
}
}
This is what you were telling?
Abid KhanPosted Jan 19, 2015, 8:00 AM
Abid KhanPosted Jan 19, 2015, 7:56 AM
i used separate try catch block in every method. in catch block of m4() i called m3 and catch block of m3() i called m2(). and in m2() i called m1(). i given this answer to interview but he is not convinced. i think he expected the answer that is provided by you...and that is efficient.
Thank you very much...
Vikram AgrawalPosted Jan 19, 2015, 4:38 AM
Try this:
public void m4()
{
try
{
//your code here
}
catch(Exception ex)
{
m3();
}
}
public void m3()
{
try
{
//your code here
}
catch(Exception ex)
{
m2();
}
}
public void m2()
{
try
{
//your code here
}
catch(Exception ex)
{
m1();
}
}
public void m1()
{
try
{
//your code here
}
catch(Exception ex)
{
// code : When Error occure
}
}
I think here is no need for nesting of try catch block.
Every method have own try catch so it will not give you any error.
Thanks.
Piyush PansuriyaPosted Jan 19, 2015, 4:27 AM
Use Try Catch for that :
Try
{
M4();
}
Catch(Exception Ex)
{
Try
{
M3();
}
Catch(Exception Ex)
{
Try
{
M2();
}
Catch(Exception Ex)
{
Try
{
M1();
}
Catch(Exception Ex)
{
Console.WriteLine(Ex.error);
}
}
}
}