Bubbling an Exception to the UI

P

pamelafluente

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
 
C

Chris Dunaway

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
 
P

pamelafluente

Thanks Chris ,

that's exactly what I needed.

Ciao,


-P

Chris Dunaway ha scritto:
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
 
P

Phill W.

- what is most recommandable:
Catch ex As Exception
Throw

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

pamelafluente

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

(I am very sorry I caused replicated discussion.)


-P

Phill W. ha scritto:
- what is most recommandable:
Catch ex As Exception
Throw

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top