473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bubbling an Exception to the UI

Hi guys,
[sorry I posted it on ASP by error]

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

pa***********@libero.it wrote:
Hi guys,
[sorry I posted it on ASP by error]

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 ?
You would probably be best served by deriving your own Exception type
and then when you catch an exception in your Try block, assign it to
the InnerException of your exception type, either using the constructor
or just assigning it. That should preserve the stack trace. In my
contrived example below, the exception pointed to by ex gets assigned
to the Inner Exception property of the MyNewException class.

Try
'some code
Catch ex As Exception
Throw New MyNewExceptionType("Custom message", ex)
End Try

Sep 25 '06 #2
Thanks Chris ,

that's exactly what I needed.

Ciao,
-P

Chris Dunaway ha scritto:
pa***********@libero.it wrote:
Hi guys,
[sorry I posted it on ASP by error]

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 ?

You would probably be best served by deriving your own Exception type
and then when you catch an exception in your Try block, assign it to
the InnerException of your exception type, either using the constructor
or just assigning it. That should preserve the stack trace. In my
contrived example below, the exception pointed to by ex gets assigned
to the Inner Exception property of the MyNewException class.

Try
'some code
Catch ex As Exception
Throw New MyNewExceptionType("Custom message", ex)
End Try
Sep 25 '06 #3
pa***********@libero.it wrote:
- what is most recommandable:
Catch ex As Exception
Throw
or
Catch ex As Exception
Throw ex
/As given/, neither.
Don't catch an exception unless you intend to do something /useful/ with
it. Simply re-throwing it doesn't count as useful. But this is only
"sample" code, so ...

If you must re-throw the Exception, just use "Throw". That retains all
the Call Stack information contained within the Exception. "Throw ex"
loses it.

I would only use "Throw ex" from the entry point of a library, where I
don't necessarily want the Outside World to know all the innards of my
code.
- What about I just to want to add some comments in the error message,
/Don't/ use the Message to store additional information.
It's far too easy to change a literal in your code and, in doing so,
break [lots of] code elsewhere that's looking for whatever string was in
there previously.

Create a Custom Exception class and add your extra properties to that.

HTH,
Phill W.
Sep 26 '06 #4
Thanks Phill for the good advices. They are very helpful.

(I am very sorry I caused replicated discussion.)
-P

Phill W. ha scritto:
pa***********@libero.it wrote:
- what is most recommandable:
Catch ex As Exception
Throw
or
Catch ex As Exception
Throw ex

/As given/, neither.
Don't catch an exception unless you intend to do something /useful/ with
it. Simply re-throwing it doesn't count as useful. But this is only
"sample" code, so ...

If you must re-throw the Exception, just use "Throw". That retains all
the Call Stack information contained within the Exception. "Throw ex"
loses it.

I would only use "Throw ex" from the entry point of a library, where I
don't necessarily want the Outside World to know all the innards of my
code.
- What about I just to want to add some comments in the error message,

/Don't/ use the Message to store additional information.
It's far too easy to change a literal in your code and, in doing so,
break [lots of] code elsewhere that's looking for whatever string was in
there previously.

Create a Custom Exception class and add your extra properties to that.

HTH,
Phill W.
Sep 26 '06 #5

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

Similar topics

14
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild....
1
by: Shawn Melton | last post by:
Does a caught exception still bubble up and will a destructor execute if an exception halts execution?
5
by: Ben R. | last post by:
Hi, I've got the following code sample to reproduce my issue: Public Class Form1 Inherits System.Windows.Forms.Form Shared Sub main() Try Application.Run(New Form1)
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: Nick | last post by:
Hi there, This probably wont make much sense but I'm getting an unhandled exception being thrown in the following line... Try While True Redraw control as necessary End While Catch ex as...
11
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 Throw
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...
4
by: jay.meerdink | last post by:
I have a simple app that opens in Program.cs with try { Application.Run(new Shell()); } catch (Exception ex) { LogErr(ex) }
0
by: Ben Crinion | last post by:
Hi Im having a bit of a problem with an exception that doesnt seem to be bubbling up from a user control to its containing user control. The exception can occur on an event fired by a user...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.