473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exception Handling Q

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
4 1120
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.