Solved

Visual Basic and how to throw exceptions

Posted on 2013-11-08
4
304 Views
Last Modified: 2013-11-11
I am recompiling vb code from vs2003 to vs2010.
One of the warnings is telling me not to "throw new exception".
so I changed it to a more specific to the situation like "throw new FormatException".
 
But through my research, I could see some blogs that advise you also never to use ApplicationException.  would that also be because it is too general?  or is there another reason why you shouldn't use that type of exception?  

thanks for helping me!
0
Comment
Question by:ToolTimeGang
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39633641
According to http://msdn.microsoft.com/en-us/library/system.applicationexception(v=vs.110).aspx:

If you are designing an application that needs to create its own exceptions, you should derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.

So...

would that also be because it is too general?

...I would say yes.

You might also condsider the article referenced by that documentation:  Best Practices for Handling Exceptions.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 175 total points
ID: 39635241
A good thing to do, specially if the exception is thrown from a dll, is to create your own Exception class, derived from Exception.

Public Class PhotoException
    Inherits Exception

End Class

You might even have many different classes for different types of exception, just as Microsoft does in the framework. SqlException and IOException both come from System.Data.dll.

This way, the receiver of the exception has a better idea of where to look for a possible solution.

Another advantage of defining your own exception class is that you can add properties that could provide extra information to the receiver, such as the SqlException that provides you with the name of the server, the stored procedure that triggered the exception, as well as the line in the procedure where the exception occurred.
0
 

Author Comment

by:ToolTimeGang
ID: 39638700
Thank you.  This was helpful!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39638906
This way, the receiver of the exception has a better idea of where to look for a possible solution.
Defining a custom exception offers no more information on where the exception occurred versus using one of the existing exceptions. This is what the stack trace provides, which all Exceptions will have anyway.

As mentioned in the article I referenced above, creating your own new exception type allows the programmer to take programmatic action based on the type of exception being raised. For example, you can easily recover from a FormatException (when trying to convert strings to other types), but encountering a SqlException when your program is trying to retrieve configuration settings from a database, then you probably cannot continue execution. If you're not going to be taking any programmatic action (e.g. try/catch, logging, etc.) based on what kind of exception occurred, then it is extraneous to create a new exception type.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a form which is copy of a form in vb.net 2 34
Format column on datatable 7 28
Events in static methods 3 50
Variable Event ? 3 20
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now