Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Visual Basic and how to throw exceptions

Posted on 2013-11-08
4
Medium Priority
?
310 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 700 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

927 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