Solved

Visual Basic and how to throw exceptions

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  This was helpful!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

10 Experts available now in Live!

Get 1:1 Help Now