By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,422 Members | 1,455 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,422 IT Pros & Developers. It's quick & easy.

Exception Handling Q

P: n/a
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;
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Thu, 5 Feb 2004 14:31:24 -0500, 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;


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

Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #2

P: n/a
TB

----- 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

Nov 15 '05 #3

P: n/a
Your code does not compile. The line after the throw is never reached!

"TB" <an*******@discussions.microsoft.com> a écrit dans le message de
news:26**********************************@microsof t.com...

----- 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;
----------------------------

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.

-- TB

Nov 15 '05 #4

P: n/a
"Vai2000" <no****@microsoft.com> wrote in message
news:O0**************@TK2MSFTNGP09.phx.gbl...

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;
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.