473,800 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making your own exception?

Throw new Excption ("bla, bla, bla")
....
Catch E as Exception
If E.Message.Equal s("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
"MyExceptio n"-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?

Nov 20 '05 #1
3 3192
"Jarod_24" <ja******@hotma il.com> schrieb
Throw new Excption ("bla, bla, bla")
...
Catch E as Exception
If E.Message.Equal s("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
"MyExceptio n"-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?'"
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
* "Jarod_24" <ja******@hotma il.com> scripsit:
Throw new Excption ("bla, bla, bla")
...
Catch E as Exception
If E.Message.Equal s("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
"MyExceptio n"-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'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Jarod,
In addition to the other comments.

These are the guidelines that I try to follow:

http://msdn.microsoft.com/library/de...Guidelines.asp

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

http://msdn.microsoft.com/library/de...rp08162001.asp

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

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

Hope this helps
Jay

"Jarod_24" <ja******@hotma il.com> wrote in message
news:5d******** *************** *******@news.te ranews.com... Throw new Excption ("bla, bla, bla")
...
Catch E as Exception
If E.Message.Equal s("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
"MyExceptio n"-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?

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2348
by: Mark | last post by:
I'm betting it me. Here is the simple schema I'm using: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="1.0"> <xs:annotation> <xs:appinfo>
5
1225
by: Nick Keighley | last post by:
Hi, In the beginning the code was:- BRUSHH new_brush = create_brush() BRUSHH old_brush = select_brush (new_brush) draw(...) select_brush (old_brush) destroy_brush (new_brush)
1
259
by: Jarod_24 | last post by:
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:
1
2429
by: Chris Langston | last post by:
We are seeing a very frustrating intermittent problem. We are making Synchronious web service method calls from a .NET windows Service using the following syntax WebRequest.getResponse call it occasionally fails with the following error: 1) Exception Information ********************************************* Exception Type: System.Net.WebException Status: ConnectFailure
37
2793
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
18
1741
by: dbahooker | last post by:
team i'm having a tough time getting these data readers to work correctly I'd just like to be able to centralize my GetDataReader functions; so i can pass a simple SQL statement and be passed back a valid DataReader object. I've been slaving with this all day long. I've been trying 'new' and not new and just slaving over this; im calling it an early day..
14
7540
by: Ben Voigt | last post by:
Under certain circumstances I get: First-chance exception at 0x7c812a5b (kernel32.dll) in LTMGUI.exe: 0xC0020001: The string binding is invalid. First-chance exception at 0x7c812a5b (kernel32.dll) in LTMGUI.exe: Microsoft C++ exception: at memory location 0x00000000.. Unhandled exception at 0x7c812a5b (kernel32.dll) in LTMGUI.exe: 0xC0020001: The string binding is invalid. Googling this exception brought me to a blog posting by Chris...
12
2572
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of reference count for particular class. Thanx in advance for your inputs.
11
6262
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as the app is concerned). The problem is, If someone makes a typo, they may get an unexpected error due accidentally calling the original attribute instead of the wrapped version. Does anyone have a simple solution for this?
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.