How to handle the exception occured in catch block? (If a exception occurs in catch block,which statement is executed next?)

Questions by jyothsna80

Showing Answers 1 - 5 of 5 Answers

Sameeksha

  • Dec 2nd, 2005
 

The exception in catch blck may be handled using the same try--catch--finally structure.

In case an exception generated in catch block is not handled in the same block, it travels its usual way -- to the parent of the current method.

Here is a piece of code written in a web form which demonstrates exception handling in catch block and what happens if exception is not handled in catch:

private void Button1_ServerClick(object sender, System.EventArgs e)

{

try

{

f();

}

catch (Exception exfinal)

{

Label3.ForeColor = System.Drawing.Color.BlueViolet;

Label3.Text = exfinal.Message + "::" + exfinal.Source + "::::::::" + exfinal.InnerException.Message;

}

}

private void f()

{

try

{

Exception ex = new Exception("Main Exception");

throw ex;

}

catch (Exception ex)

{

//another exception

try

{

Exception newex = new Exception("Exception in catch block", ex);

throw newex;

}

catch (Exception nex)

{

//Label3.Text = nex.Message + "::::" + nex.InnerException.Message;

//yet another exception, not caught here

throw nex;

}

}

}

  Was this answer useful?  Yes

first of all you have to identify within your code that what kind of exception it coud be e.g. SqlException,ArithmaticException,XmlException or any other normal C# or asp.net exception.

then exception can handled easily with the object derived from that particular class.

happy coding

  Was this answer useful?  Yes

Mukesh Sonone

  • Jul 21st, 2007
 

Since you are handling exceptions in catch block, if any error occurs that will get catch in catch{} block, but if you've furture coding done in catch{} block & if error occurs in the catch{} block, then this time no system error or runtime error will be sent as you are in catch{} block, & if you have Finally{} block also, then after this catch{} block error, directly your finally{} block will gets executed by deafult.
To catch the exception occured in your catch{} block you can write one more try{} catch{} block to catch that exception.
but there wont be any fatal error in the code.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions