473,386 Members | 1,706 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,386 software developers and data experts.

Bubbling up exceptions

Hi guys,

When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
'some code
Catch ex As Exception
Throw
End Try
or

Try
'some code
Catch ex As Exception
Throw ex
End Try
?

- What about I just to want to add some comments in the error message,
BUT
I want to keep ALL the inner exceptions (so that I can recursively
spit them to the
user when I arrive up to the UI ) How do I do that correctly ?

- And, is it true that anything after a Throw will be in any case
ignored ?

-P

Sep 24 '06 #1
11 1809
Hi,
When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
'some code
Catch ex As Exception
Throw
End Try
or

Try
'some code
Catch ex As Exception
Throw ex
End Try
I'm not a VB programmer but I don't think that the first example should even compile. If it does, then I assume that the behavior
of each sample is identicial and in that case choose whichever you like better :)
- What about I just to want to add some comments in the error message,
BUT
I want to keep ALL the inner exceptions (so that I can recursively
spit them to the
user when I arrive up to the UI ) How do I do that correctly ?
If you want to display information to the UI when you catch an exception then just do it. Don't rethrow the Exception. Only
rethrow the exception if it cannot be handled by your code. In that case, you can wrap the exception with another exception that
provides some more information about the error. The second argument supplies the InnerException for the new Exception:

Throw new Exception("An error occurred in method X trying to perform operation Y. Variable A at the time was: " & A, ex)

Also, don't just catch Exception everywhere. There are certain Exceptions in the framework that you won't be able to handle in code
and should not be caught. For this reason you should only catch those Types of exceptions that can be handled by your code.

For instance, if you are performing some operation on the System.IO.File object in your Try...Catch block then you could catch
System.IO.IOException if and only if that exception can be handled gracefully by your code. Graceful means that the state of your
program will not be corrupt, or that it can be fixed before returning to the caller. You could also update the UI, for instance,
with a simple message like "Cannot save data to file: " & ex.Message. There is no need to rethrow the IOException since it has been
handled gracefully by your code.
- And, is it true that anything after a Throw will be in any case
ignored ?
Yes, anything after a Throw statement within the same method body will not be executed.

--
Dave Sexton
Sep 24 '06 #2

Dave Sexton ha scritto:
Hi,
When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
'some code
Catch ex As Exception
Throw
End Try
or

Try
'some code
Catch ex As Exception
Throw ex
End Try

I'm not a VB programmer but I don't think that the first example should even compile. If it does, then I assume that the behavior
of each sample is identicial and in that case choose whichever you like better :)
Thanks Dave. [Actually I posted here by error. Sorry. The message was
for the VB group.]

Anyway, on this point I have been confused by some statement in this
article:
http://www.codeproject.com/dotnet/ex...tpractices.asp

where the author says that
Throw ex
is wrong and I should always use
Throw

So I 'd like to hear opinions on that.... Thank you :)

-P
--
Dave Sexton
Sep 24 '06 #3
Hi,
where the author says that
Throw ex
is wrong and I should always use
Throw
Throw without ex, at least in C#, is used when you do not assign the exception to a variable:

try
{
// do something
}
catch // no variable assignment
{
throw; // throw exception in current scope
}

I'm not sure if there is an equivalent in VB.

In C# the following code actually does compile by assuming that throw refers to the exception caught in the current scope, just like
in the code above. This works regardless of whether it is assigned to a variable.
Catch ex As Exception
Throw
End Try
That code will function exactly as "Throw ex" in your other example. I don't think it matters which one you choose.

--
Dave Sexton

<pa***********@libero.itwrote in message news:11**********************@i3g2000cwc.googlegro ups.com...
>
Dave Sexton ha scritto:
>Hi,
When bubbling some exception up to some interface handlers

- what is most recommandable:

Try
'some code
Catch ex As Exception
Throw
End Try
or

Try
'some code
Catch ex As Exception
Throw ex
End Try

I'm not a VB programmer but I don't think that the first example should even compile. If it does, then I assume that the
behavior
of each sample is identicial and in that case choose whichever you like better :)

Thanks Dave. [Actually I posted here by error. Sorry. The message was
for the VB group.]

Anyway, on this point I have been confused by some statement in this
article:
http://www.codeproject.com/dotnet/ex...tpractices.asp

where the author says that
Throw ex
is wrong and I should always use
Throw

So I 'd like to hear opinions on that.... Thank you :)

-P
>--
Dave Sexton

Sep 24 '06 #4
Hi,

pa***********@libero.it wrote:
Thanks Dave. [Actually I posted here by error. Sorry. The message was
for the VB group.]

Anyway, on this point I have been confused by some statement in this
article:
http://www.codeproject.com/dotnet/ex...tpractices.asp

where the author says that
Throw ex
is wrong and I should always use
Throw

So I 'd like to hear opinions on that.... Thank you :)

-P
First, the CodeProject example is in C#, where "throw;" is legal. I am
not sure if it is in VB.NET.

In C#, the idea to use "throw;" instead of "throw ex;" is, as explained
in the article, to avoid modifying the stack trace.

Every exception object has a stack trace, which allows you to see where
it comes from. It is a string representation of the call stack at the
time where the exception was thrown.

However, when you do "throw ex;", it clears the stack trace and
everything appears as if the exception was thrown from there.

Consider the following code:

private void ExecuteThrow()
{
try
{
NestedMethod1();
}
catch ( Exception )
{
Console.WriteLine( "Exception caught" );
throw;
}
}

private void ExecuteThrowEx()
{
try
{
NestedMethod1();
}
catch ( Exception ex )
{
Console.WriteLine( "Exception caught" );
throw ex;
}
}

private void NestedMethod1()
{
NestedMethod2();
}

private void NestedMethod2()
{
NestedMethod3();
}

private void NestedMethod3()
{
throw new ArgumentException( "This is a dummy exception only" );
}

If I call ExecuteThrow, the stack trace is the following:

at temp.NestedMethod3() in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 77
at temp.NestedMethod2() in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 72
at temp.NestedMethod1() in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 67
at temp.ExecuteThrow() in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 48
at temp.bnExecute_Click(Object sender, EventArgs e) in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 26

If I call ExecuteThrowEx, the stack trace is:

at temp.ExecuteThrowEx() in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 61
at temp.bnExecute_Click(Object sender, EventArgs e) in
d:\GalaSoft\myhtml\GalaSoft\_current\temp.aspx.cs: line 30

I lost the whole path to NestedMethod3. That's the idea in this article.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 24 '06 #5
Hi,

Dave Sexton wrote:
Hi,
In C# the following code actually does compile by assuming that throw refers to the exception caught in the current scope, just like
in the code above. This works regardless of whether it is assigned to a variable.

>>> Catch ex As Exception
Throw
End Try


That code will function exactly as "Throw ex" in your other example. I don't think it matters which one you choose.
In C# it is not the same. Check my post about that in this same thread.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 24 '06 #6
Hi Laurent,

I see the difference now, thanks.

Code "throw" without explicitly specifying an exception so that the exception in scope is rethrown and the stack trace is preserved.
If you need to wrap the exception, however, you have no choice but to include the in-scope exception as the InnerException and the
stack trace will be rebuilt.

As a rule of thumb, I don't normally catch and rethrow without wrapping the exception. If you have nothing more to add to it then
don't catch it in the first place. The exception to that rule is if you need to log something or perform some state management when
the exception occurs, in which case using "throw" to rethrow the exception is good advice.

--
Dave Sexton

"Laurent Bugnion" <ga*********@bluewin.chwrote in message news:eS*************@TK2MSFTNGP02.phx.gbl...
Hi,

Dave Sexton wrote:
>Hi,
In C# the following code actually does compile by assuming that throw refers to the exception caught in the current scope, just
like in the code above. This works regardless of whether it is assigned to a variable.

>>>> Catch ex As Exception
Throw
End Try


That code will function exactly as "Throw ex" in your other example. I don't think it matters which one you choose.

In C# it is not the same. Check my post about that in this same thread.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Sep 24 '06 #7
Hi,

Dave Sexton wrote:
Hi Laurent,

I see the difference now, thanks.

Code "throw" without explicitly specifying an exception so that the exception in scope is rethrown and the stack trace is preserved.
If you need to wrap the exception, however, you have no choice but to include the in-scope exception as the InnerException and the
stack trace will be rebuilt.
Only the stack trace of the outer exception will start at the moment
where the outer exception is thrown. The stack trace of the
InnerException will be preserved. That's why you should include it when
you build your own exceptions

catch ( Exception ex )
{
// Do something
throw new MyOwnException( "message", ex );
}

As a rule of thumb, I don't normally catch and rethrow without wrapping the exception. If you have nothing more to add to it then
don't catch it in the first place. The exception to that rule is if you need to log something or perform some state management when
the exception occurs, in which case using "throw" to rethrow the exception is good advice.
HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 24 '06 #8
Hi Laurent,

That's exactly what I meant. Sorry if I wasn't clear enough.

--
Dave Sexton

"Laurent Bugnion" <ga*********@bluewin.chwrote in message news:eo**************@TK2MSFTNGP06.phx.gbl...
Hi,

Dave Sexton wrote:
>Hi Laurent,

I see the difference now, thanks.

Code "throw" without explicitly specifying an exception so that the exception in scope is rethrown and the stack trace is
preserved. If you need to wrap the exception, however, you have no choice but to include the in-scope exception as the
InnerException and the stack trace will be rebuilt.

Only the stack trace of the outer exception will start at the moment where the outer exception is thrown. The stack trace of the
InnerException will be preserved. That's why you should include it when you build your own exceptions

catch ( Exception ex )
{
// Do something
throw new MyOwnException( "message", ex );
}

>As a rule of thumb, I don't normally catch and rethrow without wrapping the exception. If you have nothing more to add to it
then don't catch it in the first place. The exception to that rule is if you need to log something or perform some state
management when the exception occurs, in which case using "throw" to rethrow the exception is good advice.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Sep 24 '06 #9

Laurent Bugnion ha scritto:

Thank you Laurent and Dave.

*Very* helpful and instructive discussion.

-P
Hi,

Dave Sexton wrote:
Hi Laurent,

I see the difference now, thanks.

Code "throw" without explicitly specifying an exception so that the exception in scope is rethrown and the stack trace is preserved.
If you need to wrap the exception, however, you have no choice but to include the in-scope exception as the InnerException and the
stack trace will be rebuilt.

Only the stack trace of the outer exception will start at the moment
where the outer exception is thrown. The stack trace of the
InnerException will be preserved. That's why you should include it when
you build your own exceptions

catch ( Exception ex )
{
// Do something
throw new MyOwnException( "message", ex );
}

As a rule of thumb, I don't normally catch and rethrow without wrapping the exception. If you have nothing more to add to it then
don't catch it in the first place. The exception to that rule is if you need to log something or perform some state management when
the exception occurs, in which case using "throw" to rethrow the exception is good advice.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 24 '06 #10
Hi,

Just wanted to point that I posted an article about this subject on my
blog, and a demo on my website. The demo shows the effect of using
throw; versus throw ex;

".NET: Re-throwing exceptions without amnesia"
http://geekswithblogs.net/lbugnion/a.../29/92708.aspx

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #11

"without amnesia" ? :)

Actually, more than simple amnesia, it would be an unrecoverable loss
of memory :))

Very cute Laurent !!

Ciao,

-P

Laurent Bugnion ha scritto:
Hi,

Just wanted to point that I posted an article about this subject on my
blog, and a demo on my website. The demo shows the effect of using
throw; versus throw ex;

".NET: Re-throwing exceptions without amnesia"
http://geekswithblogs.net/lbugnion/a.../29/92708.aspx

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn Melton | last post by:
Does a caught exception still bubble up and will a destructor execute if an exception halts execution?
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
3
by: Z D | last post by:
Hello, I have a remoted object which throws an exception if something goes bad. eg: throw new Exception("Something went wrong."); When I test the assembly locally on my machine (without...
4
by: Wee Bubba | last post by:
the following event handler checks to see if any parent control has attached to the event 'SearchItemSelect'. i use it for event bubbling whenever one of my search items has been selected and at...
3
by: Nathan Sokalski | last post by:
I have three LinkButtons in the HeaderTemplate of my DataList (I use them to let the user determine what to sort the list by). I am assuming that the event will be bubbled to the ItemCommand event...
7
by: comzy | last post by:
I have created an event bubbling for my pager control which is used for implementing paging in data grid. Althoug it worked very fine in .NET 1.1 it is throwing the following error after i migrated...
4
by: pamelafluente | last post by:
Hi guys, When bubbling some exception up to some interface handlers - what is most recommandable: Try 'some code Catch ex As Exception
2
by: Chuck Bowling | last post by:
In studying for the 70-316 I ran across the question below. I'm a little confused by the 'correct' answer. Why is it necessary to wrap the Validate method in a try/catch block? Doesn't the...
2
by: alhalayqa | last post by:
hi, in MSIE, you can attach onclick event to both anchor and document objects. here is some code : document.onclick = function() {alert('document clicked ...'); } <a href= " " ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.