Home > SOA Tips > .NET Developer > Handling exceptions in .NET
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Handling exceptions in .NET


Peter Aitken
12.07.2004
Rating: -1.86- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


For programmers, exceptions—that's just a fancy name for run-time errors—are a fact of life. Good programming practices can minimize exceptions, but it is impossible to prevent them altogether. Many of the causes, such as network errors or incorrectly set file permissions, are beyond the programmer's control. This excerpt from Informit explains how to filter the Catch statement to handle exceptions.


I'll use Visual Basic, in this article, but the same principles and techniques apply to C# as well.

The .Net framework implements structured exception handling. The term structured means that the code has the potential to cause an exception. Plus, the code to handle the exception is organized in structured blocks. You use the Try...Catch...End Try statement for this purpose.

The ability to filter the Catch statement is an essential part of .Net's structured exception handling. If there is no filter—or, to be more precise, there is a filter that matches all exceptions—the code in the Catch block is executed for any and all exceptions that are thrown. Such a Catch block would be written like this:

Catch ex As Exception

Within the Catch block, the object ex, an instance of the System.Exception class, will contain information about the exception. You can use any legal variable name here, although ex or e is traditional. To filter for specific exceptions, you would write the Catch statement like this:

Catch ex As ExceptionType

replacing ExceptionType with the name of the specific exception class to be caught. The .Net framework defines a hierarchy of several dozen exception classes, all derived from System.Exception, with each individual exception class encompassing related exceptions. For example, all file-related exceptions are subsumed under IoException, but for more specific control you can catch file loading exceptions with FileLoadException and FileNotFoundException. You'll find a complete diagram of the exception hierarchy in the Visual Studio documentation.

The filtering capability lets you write Catch blocks to deal with specific exceptions. This makes perfect sense, because the message to the user and the program's response will differ, depending on the type of exception. Simply create a separate Catch block for each condition. It is also common practice to include a final Catch block to deal with any exceptions that fall through the preceding Catch filters. Here's an example:

Catch ex As FileNotFoundException
' Code here to respond to a "File Not Found" exception.
Catch ex As FileLoadException
' Code here to respond to a "File Load" exception.
Catch ex As Exception
' Code here to respond to any exceptions not caught by previous Catch statements.

The Catch keyword, by itself, will catch any exception, just like Catch ex As Exception. The disadvantage is that you will not have an Exception object in that block to provide information about the exception and its cause.


Read the rest of this Informit article for more filtering techniques as well as ways to respond to exceptions.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Microsoft .NET Web services
Microsoft preps .NET 4.0 - framework improves on REST, MVC, JQuery support
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Microsoft .NET Web services Research

Platforms and Servers
Tracking down managed memory leaks
.NET Compact Framework graphics
The Data Access Application Block
Decision time: .NET or J2EE?
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment
Displaying errors with the error provider
VS.NET 2005 betas

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts