Board index » Visual Studio » Bubbling an Exception to the UI

Bubbling an Exception to the UI

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


-
 

Re:Bubbling an Exception to the UI



pamelafluente@libero.it wrote:

Quote
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



-

Re:Bubbling an Exception to the UI

Thanks Chris ,



that's exactly what I needed.



Ciao,





-P



Chris Dunaway ha scritto:



Quote
pamelafluente@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



-

Re:Bubbling an Exception to the UI

pamelafluente@libero.it wrote:



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



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

-

Re:Bubbling an Exception to the UI

Thanks Phill for the good advices. They are very helpful.



(I am very sorry I caused replicated discussion.)





-P



Phill W. ha scritto:



Quote
pamelafluente@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.



-