Handle exceptions properly in C#
Takeaway: Irina Medvinskaya discusses how to use exception handling in C# and explains simple ways to add code snippets to application code to prevent users from seeing certain errors.
Developers often overlook the importance of exception handling to the detriment of their own code. In this article, I discuss how to use exception handling in C#. I also explain simple ways to add code snippets to application code to prevent users from seeing certain errors, which may cause applications to shut down.
Structured exception handling
The .NET Framework provides a standard mechanism for error reporting called structured exception handling. The mechanism relies on exceptions to report an error in the application.
In .NET, exceptions are classes that provide error information. You are expected to write code in a way that watches for exceptions generated by code and then deal with the exceptions in an appropriate manner.
When working with exceptions in C#, you need to take care of three components in the code:
- The block of code that may result in an exception (this is also referred to as throwing an exception).
- The block of code that will be executed in case there is an exception raised when the block of code is processed (this is also referred to as catching an exception).
- The block of code that will be executed after the exception is processed (optional) (this is also referred to as the finally block).
Exception class
Exception classes in the .NET Framework are derived from a System.Exception class. Most utilized members of the class are listed below:
- HelpLink is a link to a help file that provides information about the exception.
- Message is text that specifies the details of an error.
- Source is the name of the object or application that caused the exception.
- StackTrace is the list of the method calls on the stack.
- TargetSite is the name of the method that threw the exception.
Try/Catch/Finally block
To handle an exception in C#, a Try/Catch/Finally block is utilized.
- Try statement specifies that you have a block of code that you should watch for exceptions thrown in while executing.
- Catch statement specifies what code should be executed when an exception occurs.
- Finally statement specifies the block of code that should be executed after a try code block executes. This block of code will be executed regardless of whether an exception occurs. In practice, it's often utilized for any clean up code you may require.
Catching all exceptions
.NET allows you to catch any exception that occurs in a particular block of code; however, you can also specify the exact exceptions to catch. Listing A shows an example of catching any exception.
The code contains an error in order to demonstrate catching an exception. In Listing A, I catch any exception that may take place after the try keyword; I do this by declaring the variable ex of an Exception type. Regardless of the kind of error in this code, the statement in the catch block will execute. In addition, even if an error occurs (which it will in this case), the code in the finally block will execute.
Catching specific classes of exceptions
Listing B shows an example of catching a specific exception. The code contains an error in order to demonstrate catching an exception. In Listing B, I catch a specific exception (DivideByZeroException), which takes place when the code is executed; I do this by declaring the variable ex of a DivideByZeroException type. Only this kind of error would lead the execution to the statement after the catch keyword. In addition, even if an error occurs (which it will in this case), the code in the finally block will execute.
Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a Project Manager at Citigroup.
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- The True Costs of Virtual Server Solutions VMware Discover ways to streamline and simplify your assessment of the total acquisition costs of a server virtualization environment. Download Now
- ENTERPRISE SECURITY GETS SMART Trend Micro IT security professionals are increasingly more concerned about the ... Download Now
- Using Red Hat Enterprise Linux AS to Achieve Highly Available, Load-Balanced Clusters Dell The Red Hat Enterprise Linux AS operating system integrates Cluster ... Download Now
- ENTERPRISE SECURITY MUST GET SMARTER Trend Micro Internet security threats are always changing but what types of attacks ... Download Now
- Tuning Oracle on Windows for Maximum Performance on PowerEdge Servers Dell Tuning is the art and science of modifying and reconfiguring your system ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


