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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pradeep ShetRank #595 · 4K reputationPosted Jan 18, 2015, 11:24 PM EST
try
{
m4();
}
catch
{
try
{
m3();
}
catch
{
try
{
m2();
}
catch
{
m1();
}
}
}
This is what you were telling?
Vikram AgrawalRank #4,713 · 329 reputationPosted Jan 18, 2015, 11:38 PM EST
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 PansuriyaRank #454 · 5.5K reputationPosted Jan 18, 2015, 11:27 PM EST
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);
}
}
}
}
Abid KhanRank #8,523 · 220 reputationPosted Jan 19, 2015, 2:56 AM EST
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...
Abid KhanRank #8,523 · 220 reputationPosted Jan 19, 2015, 3:00 AM EST