Exception Handling Q

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

Hi All, my errMsg string never gets initialized when there is an exception.
Can someone explain me why?

Thanks

string errMsg=null;
try
{
MyFunction();
}
catch(Exception e)
{

throw new Exception("Failed in MyFunction",e.InnerException);
errMsg="Some error:
}

return errMsg;
 
Hi All, my errMsg string never gets initialized when there is an exception.
Can someone explain me why?

string errMsg=null;
try
{
MyFunction();
}
catch(Exception e)
{
throw new Exception("Failed in MyFunction",e.InnerException);
errMsg="Some error:
}
return errMsg;

The Exception that you throw inside of your handler causes the
processing to terminate there.

Tim
 
----- Vai2000 wrote: ----

Hi All, my errMsg string never gets initialized when there is an exception
Can someone explain me why

Thank

string errMsg=null
tr

MyFunction()

catch(Exception e


throw new Exception("Failed in MyFunction",e.InnerException)
errMsg="Some error


return errMsg
----------------------------

The reason is that when you re-throw the exception (wrapped inside your new one) the thread of execution completely skips both your assignment to 'errMsg' as well as the following return and goes, instead, to the next higher level catch. If there is no higher level catch then you'll get the runtime's "Uncaught Exception" message box

Just move the assignment to 'errMsg' above the throw

-- T
 
Your code does not compile. The line after the throw is never reached!

TB said:
----- Vai2000 wrote: -----

Hi All, my errMsg string never gets initialized when there is an exception.
Can someone explain me why?

Thanks

string errMsg=null;
try
{
MyFunction();
}
catch(Exception e)
{

throw new Exception("Failed in MyFunction",e.InnerException);
errMsg="Some error:
}

return errMsg;
new one) the thread of execution completely skips both your assignment to
'errMsg' as well as the following return and goes, instead, to the next
higher level catch. If there is no higher level catch then you'll get the
runtime's "Uncaught Exception" message box.
 
As you've already been told everything after the throw is not eveluated
anymore, instead the next exception handler kicks in. If you don't have
another exception handler protecting the code you posted somewhere higher in
the call stack of your application, the CLR will handle the exception for
you.

Judging by the return statement your intent seems to be to handle the
exception and continue normally. In that case you do not throw another
exception, you just catch and take the error message:

string errMsg=null;
try
{
MyFunction();
}
catch(Exception e)
{
//
errMsg = "MyFunction failed and this is the lame excuse it returned: " +
e.Message;
}
return errMsg;


If you do want to toss the exception on to the next level and you have some
code after MyFunction() that needs to be executed regardless the success of
MyFunction you may add a finally clause:

string errMsg=null;
try
{
MyFunction();
}
catch(Exception e)
{
// this will only run in case MyFunction throws an exception
errMsg = "MyFunction failed and this is the lame excuse it returned: " +
e.Message;

// throw new Exception("rethrow", e);
}
finally
{
// this will always run, with or without a rethrow
;
}
// this will not run if an exception is rethrown
return errMsg;
 
Back
Top