Making your own exception?

  • Thread starter Thread starter Jarod_24
  • Start date Start date
J

Jarod_24

Throw new Excption ("bla, bla, bla")
....
Catch E as Exception
If E.Message.Equals("bla, bla, bla") Then ...


Mostly i just use the standard "Exception"-object whenever i throw an
exception, but it would be nice sometimes to make you own
"MyException"-object that you
can then easier Catch. using:

Catch Exp as MyException
....

Whenever i use the standard exception-object i check to see if the E.Message
equals the one i made, but since this isn't a very good way of coding, so
how do i make my own Exception-object. Do i need to make a new class that
inherits/implements from the Exception.class, or is there a way to do it
without starting making a exception-class for each object you want a
specialized exception for. Someting you do when you trow the exception for
example?
 
Jarod_24 said:
Throw new Excption ("bla, bla, bla")
...
Catch E as Exception
If E.Message.Equals("bla, bla, bla") Then ...


Mostly i just use the standard "Exception"-object whenever i throw
an exception, but it would be nice sometimes to make you own
"MyException"-object that you
can then easier Catch. using:

Catch Exp as MyException
...

Whenever i use the standard exception-object i check to see if the
E.Message equals the one i made, but since this isn't a very good way
of coding, so how do i make my own Exception-object. Do i need to
make a new class that inherits/implements from the Exception.class,
or is there a way to do it without starting making a exception-class
for each object you want a specialized exception for. Someting you do
when you trow the exception for example?

Has been discussed here in the past days. See subject "Error handling -
custom 'throw?'"
 
* "Jarod_24 said:
Throw new Excption ("bla, bla, bla")
...
Catch E as Exception
If E.Message.Equals("bla, bla, bla") Then ...


Mostly i just use the standard "Exception"-object whenever i throw an
exception, but it would be nice sometimes to make you own
"MyException"-object that you
can then easier Catch. using:

Catch Exp as MyException
...

Whenever i use the standard exception-object i check to see if the E.Message
equals the one i made, but since this isn't a very good way of coding, so
how do i make my own Exception-object. Do i need to make a new class that
inherits/implements from the Exception.class, or is there a way to do it
without starting making a exception-class for each object you want a
specialized exception for. Someting you do when you trow the exception for
example?

This depends on various aspects:

* If there is already an appropriate exception class defined in the
.NET Framework, I would use this class instead of creating a new
class.

* If there is no class available in the .NET Framework, I would create
a custom exception class in order to make it easier to handle the
exceptions of a certain type.

Notice that it doesn't make sense to create a new exception class for
each exception -- sometimes it's better to use the same exception class,
for example, 'IOException'.
 
Jarod,
In addition to the other comments.

These are the guidelines that I try to follow:

http://msdn.microsoft.com/library/d.../html/cpconErrorRaisingHandlingGuidelines.asp

The following article provides a good template for defining well-tempered
exceptions:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp08162001.asp

Although the code is in C# it should be easily converted to VB.NET.
Catch E as Exception
If E.Message.Equals("bla, bla, bla") Then ...
Did you know you can use a When clause here?

Try
Catch ex As Exception When ex.Message.Equals("bla, bla, bla")
Catch ex As Exception When ex.Message.Equals("alb, alb, alb")
End Try

Hope this helps
Jay
 
Back
Top