Solved

Visual Basic and how to throw exceptions

Posted on 2013-11-08
4
305 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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