.NET Tip: Throwing Custom Exceptions Wisconsin

Use custom exceptions to add information that will be more meaningful to your users when exceptions occur. And, you still can maintain all the information from the original exception.

Local Companies

Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
Mtm International
(608) 848-0900
201 Prairie Heights Dr
Verona, WI
Electronic Data Systems Inc
(262) 780-6400
13890 Bishops Dr
Brookfield, WI


.NET Tip: Throwing Custom Exceptions

provided by: 
Originally published at Internet.com


Creating your own custom exceptions is very easy and allows your code to be more explicit as well as being able to provide more user-friendly error messages. To show how to use custom exceptions, I will use connecting to a database as an example. Connecting to a database is something your application probably does on a regular basis and is a common point of failure. The first step is to create the class for your custom exception that inherits from another exception class. In this case, you'll create an UnableToOpenDatabaseException based on the Exception class. Here is what the custom exception class looks like: public class UnableToOpenDatabaseException : Exception { public UnableToOpenDatabaseException() : base() { } public UnableToOpenDatabaseException(string Message) : base(Message) { } public UnableToOpenDatabaseException(string Message, Exception InnerException) : base(Message, InnerException) { } protected UnableToOpenDatabaseException(SerializationInfo Info, StreamingContext Context) : base(Info, Context) { } }

To use the custom exception, you need to check to see whether an exception is thrown when you attempt to connect to the database. In my case, I'm connecting to SQL Server, so I will check to see if a SQLException was thrown. If a SQLException is thrown, I throw the new UnableToOpenDatabaseException, passing the original exception to the new one. If an exception other than a SQLException is thrown, I simply rethrow the original exception. using (SqlConnection CN = new SqlConnection("*** Your connection string here ***")) { try { CN.Open(); } catch (SqlException ex) { // Throw the custom UnableToOpenDatabaseException throw new UnableToOpenDatabaseException("Unable to open database connection.", ex); } catch (Exception ex) { // Rethrow the original exception throw; } }

This allows the calling code to take advantage of the new exception while still allowing it to access all of the original exception information if needed. This example lumped any type of SQLException under the new exception type. If you needed more granularity in your exceptions, you could create custom exceptions for different failure conditions.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

HarrisData

262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
http://www.HarrisData.com

Regional Articles
- .NET Tip: Throwing Custom Exceptions Appleton WI
- .NET Tip: Throwing Custom Exceptions Baraboo WI
- .NET Tip: Throwing Custom Exceptions Beaver Dam WI
- .NET Tip: Throwing Custom Exceptions Beloit WI
- .NET Tip: Throwing Custom Exceptions Brookfield WI
- .NET Tip: Throwing Custom Exceptions Burlington WI
- .NET Tip: Throwing Custom Exceptions Cedarburg WI
- .NET Tip: Throwing Custom Exceptions Chippewa Falls WI
- .NET Tip: Throwing Custom Exceptions Cudahy WI
- .NET Tip: Throwing Custom Exceptions De Pere WI
- .NET Tip: Throwing Custom Exceptions Delavan WI
- .NET Tip: Throwing Custom Exceptions Eau Claire WI
- .NET Tip: Throwing Custom Exceptions Elkhorn WI
- .NET Tip: Throwing Custom Exceptions Fond Du Lac WI
- .NET Tip: Throwing Custom Exceptions Fort Atkinson WI
- .NET Tip: Throwing Custom Exceptions Franklin WI
- .NET Tip: Throwing Custom Exceptions Green Bay WI
- .NET Tip: Throwing Custom Exceptions Hartland WI
- .NET Tip: Throwing Custom Exceptions Janesville WI
- .NET Tip: Throwing Custom Exceptions Kaukauna WI
- .NET Tip: Throwing Custom Exceptions Kenosha WI
- .NET Tip: Throwing Custom Exceptions La Crosse WI
- .NET Tip: Throwing Custom Exceptions Lake Geneva WI
- .NET Tip: Throwing Custom Exceptions Manitowoc WI
- .NET Tip: Throwing Custom Exceptions Marinette WI
- .NET Tip: Throwing Custom Exceptions Marshfield WI
- .NET Tip: Throwing Custom Exceptions Menasha WI
- .NET Tip: Throwing Custom Exceptions Menomonee Falls WI
- .NET Tip: Throwing Custom Exceptions Menomonie WI
- .NET Tip: Throwing Custom Exceptions Merrill WI
- .NET Tip: Throwing Custom Exceptions Middleton WI
- .NET Tip: Throwing Custom Exceptions Milwaukee WI
- .NET Tip: Throwing Custom Exceptions Mosinee WI
- .NET Tip: Throwing Custom Exceptions Mukwonago WI
- .NET Tip: Throwing Custom Exceptions Muskego WI
- .NET Tip: Throwing Custom Exceptions Neenah WI
- .NET Tip: Throwing Custom Exceptions New Berlin WI
- .NET Tip: Throwing Custom Exceptions Oak Creek WI
- .NET Tip: Throwing Custom Exceptions Oconomowoc WI
- .NET Tip: Throwing Custom Exceptions Onalaska WI
- .NET Tip: Throwing Custom Exceptions Oshkosh WI
- .NET Tip: Throwing Custom Exceptions Pewaukee WI
- .NET Tip: Throwing Custom Exceptions Racine WI
- .NET Tip: Throwing Custom Exceptions Rhinelander WI
- .NET Tip: Throwing Custom Exceptions Rice Lake WI
- .NET Tip: Throwing Custom Exceptions River Falls WI
- .NET Tip: Throwing Custom Exceptions Schofield WI
- .NET Tip: Throwing Custom Exceptions Shawano WI
- .NET Tip: Throwing Custom Exceptions Sheboygan WI
- .NET Tip: Throwing Custom Exceptions South Milwaukee WI
- .NET Tip: Throwing Custom Exceptions Stevens Point WI
- .NET Tip: Throwing Custom Exceptions Sturgeon Bay WI
- .NET Tip: Throwing Custom Exceptions Sun Prairie WI
- .NET Tip: Throwing Custom Exceptions Superior WI
- .NET Tip: Throwing Custom Exceptions Thiensville WI
- .NET Tip: Throwing Custom Exceptions Two Rivers WI
- .NET Tip: Throwing Custom Exceptions Watertown WI
- .NET Tip: Throwing Custom Exceptions Waukesha WI
- .NET Tip: Throwing Custom Exceptions Waupaca WI
- .NET Tip: Throwing Custom Exceptions Wausau WI
- .NET Tip: Throwing Custom Exceptions West Bend WI
- .NET Tip: Throwing Custom Exceptions Whitewater WI
- .NET Tip: Throwing Custom Exceptions Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Industrial Goods & Services Software
Business Services Family Insurance Technology
Career Financial Services Internet Telecommunications
Cars Food & Beverage Legal Transportation & Logistics
Computer Hardware Health Real Estate Travel
Construction Home Services Retail & Consumer Services Wedding
Education