Error Handling in C#
Internet is a worldwide phenomenon and has become a necessity rather than a luxury today. And when we use the internet, it is not uncommon to come across desktop applications, web applications, mobile applications, etc. All these applications are built using different programming languages and one such programming language is C#.
While programming such applications, developers encounter several odd circumstances where the program does not work the way it is supposed to. A person with limited programming knowledge will think this to be an error in the program. But if you are an experienced developer, you will know that this is an exception that has occurred during the flow of the program. To handle such unforeseen circumstances, most programming languages including C# empowers the developers to handle them efficiently.
In this article, we are going to find out what is an exception and how it is handled in C#.
Table of content:
- What is an Exception?
- Anatomy of exceptions in C#
- How to create custom exception types?
- Exception logging practices
- Wrapping up
What is an Exception?
Exceptions typically occur when the application is in an execution state. The key difference between an exception and an error is: Errors are simply unwanted breaks in the code while exceptions are the result of various unwanted incidents happening during the run time. Some common exceptions are out of memory exception, null reference exception, and index out of range exception.
Since exceptions are usually expected to occur in most programs, developers are prepared to handle them well. Through effective exception handling, you can ensure that the program continues to run without breakage. You can even perform various operations when exceptions occur, like logging the exception, performing some arithmetic operations, database query, etc, depending on the use case of the application.
Anatomy of exceptions in C#
The code flow when exceptions occur is handled by transferring control from one part of the code to another. On reaching the exception, the code is interrupted for that block, and flow is handed back to the parent try-catch block. The statements used to handle the exception are:
- try – This block contains the part of code that is expected to cause an exception. This block of code will always execute (even partially, until an exception occurs). When code inside the try block throws an exception, its corresponding catch block will handle it.
- catch – This block is executed when the try block throws an exception. Catch block doesn't need to be executed every time. Inside this block, you can handle an exception, log it, or perform some operation.
- finally – This is the block of the code that will be executed independently of the fact whether an exception is thrown or not. Some operations like deallocation or memory or object are usually performed in this.
- throw – This is the keyword used to manually raise an exception on basis of certain conditions. These manually thrown exceptions can be handled by using a try-catch block based on the use case.
The above code shows the basic use of ‘try-catch’ and ‘finally’ blocks. If a web exception occurs in the try block, the flow goes to the first catch block. For any other exception, it goes to the second catch block, and then the ‘finally’ block is executed.
The above code is the use of a try-catch block with filters. The filters give us additional control over the catch block and we can fine-tune the exceptions we want to catch.
How to create custom exception types?
The exceptions in C# are a block of code predefined as a class and they are inherited from System.Exception class. We can create our custom exceptions but it is typically done when we want to catch and handle the exception differently. By defining custom exception, we can use custom methods or attributes which are typically not part of the standard exception.
You can refer to the below code to create a custom exception.
Exception logging practices
A key component to handle exceptions properly is logging the exception. It gives the ability to find the cause of the exception without running the code in debugger mode. It is recommended to use NLog, Serilog, or log4net framework as these give us the ability to log in to the file, or other targets like database and email.
This code on throwing the exception in a try block enters the catch block and will log the string (“Some Reason” in this case) along with the traceback for the exception. This is very critical to find the cause of the problem.
Exception logging in a file is recommended. But if an application is running in production and tends to have multiple clients, it may not be enough. Logging huge chunks of data in a file will make it practically impossible to find the exception traversing through the file. For the development team, an error monitoring service is required as they have the following benefits.
- Log exception in a central location
- Easy to search and view the exception
- Unique ID to identify the exception
- Give various options like alert, email, call on high error or exceptions for better reliability
In this post, we have got a glimpse into the world of C# and how to handle exceptions in it. We have also seen some of the best practices in exception handling. Next time you encounter an exception in your C# code, you know how to handle it effectively.