|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Custom exceptionsfrom ApplicationException. Unlike the .NET framework exceptions, I cannot set my Message property in the first line of the constructor like this: Class CustomException(Message as string) MyBase.New(Message) End Class I cannot do this because I need to do some other stuff before I calculate the Message property. But after that line right there, there is no way to set the Message property. I tried overriding the ToString() property like this: Overrides Function ToString() as string Return "My custom message" End Function This doesn't work, when use the "Throw" statement on an instance of my CustomException. Instead the exception window just says "Error in the application". Any ideas on what I am doing wrong? (TIA) >Following guidelines from MSDN, I created my custom exceptions by inheriting Then you may want to read this>from ApplicationException. http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx >Class CustomException(Message as string) You can put the calculations in a shared method and call it as part of> MyBase.New(Message) >End Class > >I cannot do this because I need to do some other stuff before I calculate >the Message property. But after that line right there, there is no way to set >the Message property. the call to the base constructor. MyBase.New(CalculateStuff(Message)) .... Shared Function CalculateStuff(message As String) As String Mattias -- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. After reading that blog, atleast I know I'm not the only one who fails to
find a good use for that class. I'll use exception until I have a good reason not to. Not sure what you are doing, but I cannot see a reason why you cannot
calculate the information before instantiating the Exception object. Something like: Try 'Try some stuff Catch 'Oops a problem happened here, so lets calculate some stuff 'Now we are finished, let's throw our custom exception End Try --- Gregory A. Beamer MVP; MCP: +I, SE, SD, DBA *************************** Think Outside the Box! *************************** Show quoteHide quote "vbMental" wrote: > Following guidelines from MSDN, I created my custom exceptions by inheriting > from ApplicationException. Unlike the .NET framework exceptions, I cannot set > my Message property in the first line of the constructor like this: > > Class CustomException(Message as string) > MyBase.New(Message) > End Class > > I cannot do this because I need to do some other stuff before I calculate > the Message property. But after that line right there, there is no way to set > the Message property. > I tried overriding the ToString() property like this: > > Overrides Function ToString() as string > Return "My custom message" > End Function > > This doesn't work, when use the "Throw" statement on an instance of my > CustomException. Instead the exception window just says "Error in the > application". > > Any ideas on what I am doing wrong? (TIA) You're right, I could calculate before throwing the custom exception but in
that case I might as well be simply throwing an ApplicationException and setting the message in the constructor. In other words, that would make my CustomException useless except for it's type. What is the value (as developers) for us to inherit from the ApplicationException class as stated if you are limited to defining the message in the first line of the constructor? I could be overstating the problem, but here's what I had in mind: Creating an exception class that was more user friendly by me passing it some parameters such as the business process that was attempting to be performed, what went wrong (in terms simpler than the exception.message property that caused), and what could possibly done to resolve it... I wanted to pass in those properties (sort-of) and have it displayed in the message property, but i'm afraid that if the dialog that pops up when I use the THROW statement wont show the message (as I have overridden it - it only shows "Error in the application.") then I'm afraid that my more 'helpful' message would be surpressed. Show quoteHide quote "Cowboy (Gregory A. Beamer) - MVP" wrote: > Not sure what you are doing, but I cannot see a reason why you cannot > calculate the information before instantiating the Exception object. > Something like: > > Try > 'Try some stuff > Catch > 'Oops a problem happened here, so lets calculate some stuff > 'Now we are finished, let's throw our custom exception > End Try > > --- > > Gregory A. Beamer > MVP; MCP: +I, SE, SD, DBA > > *************************** > Think Outside the Box! > *************************** > > "vbMental" wrote: > > > Following guidelines from MSDN, I created my custom exceptions by inheriting > > from ApplicationException. Unlike the .NET framework exceptions, I cannot set > > my Message property in the first line of the constructor like this: > > > > Class CustomException(Message as string) > > MyBase.New(Message) > > End Class > > > > I cannot do this because I need to do some other stuff before I calculate > > the Message property. But after that line right there, there is no way to set > > the Message property. > > I tried overriding the ToString() property like this: > > > > Overrides Function ToString() as string > > Return "My custom message" > > End Function > > > > This doesn't work, when use the "Throw" statement on an instance of my > > CustomException. Instead the exception window just says "Error in the > > application". > > > > Any ideas on what I am doing wrong? (TIA)
Maximum number of threads in a process
run app remotely? Get environment from remote machine Forcing traffic to a proxy server Socket Send and Receive Thread Safety windows forms leak Load framework first time Assign attributes to a property at runtime ? Unix and .NET ServiceBase.OnStop doesn't work properly |
|||||||||||||||||||||||