|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bubbling an Exception to the UI[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 pamelaflue***@libero.it wrote:
Show quoteHide quote > Hi guys, You would probably be best served by deriving your own Exception type> [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 ? > 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 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 pamelaflue***@libero.it wrote:
> - what is most recommandable: /As given/, neither.> Catch ex As Exception > Throw > or > Catch ex As Exception > Throw ex 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. 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.
UTF8 Encoding and MP3 tags
SQL Statement for limiting the number of detail retrieved in Access 2000? Use items from... SQL DISTINCT COUNT Constant - InDebugMode NullReferenceException UserControls in a different directory Word Document or Report...which to use? Using cryptographic streams with XML Deploying programs with net framework 2 onto framework 1 |
|||||||||||||||||||||||