Home All Groups Group Topic Archive Search About

Bubbling an Exception to the UI

Author
24 Sep 2006 3:43 PM
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

Author
25 Sep 2006 3:10 PM
Chris Dunaway
pamelaflue***@libero.it wrote:
Show quoteHide 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
Author
25 Sep 2006 3:13 PM
pamelafluente
Thanks Chris ,

that's exactly what I needed.

Ciao,


-P

Chris Dunaway ha scritto:

Show quoteHide quote
> pamelaflue***@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
Author
26 Sep 2006 10:40 AM
Phill W.
pamelaflue***@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.
Author
26 Sep 2006 10:53 AM
pamelafluente
Thanks Phill for the good advices. They are very helpful.

(I am very sorry I caused replicated discussion.)


-P

Phill W. ha scritto:

Show quoteHide quote
> pamelaflue***@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.