Solved

Visual Basic and how to throw exceptions

Posted on 2013-11-08
4
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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