Exception Handling in c#

Prerna Kaushik
5 min readApr 8, 2020

--

When we talk about a few of the best ways of writing code to make it more robust, Exception handling comes as one of the most important as well as the most ignored part. Exception handling is the process where we handle the run time errors so as they don’t interrupt or block the actual flow of the program.

What are Exceptions? How are Exceptions different from errors?

Exception :

An exception is an error that comes while executing the program, exceptions are basically run time errors that can’t be diagnosed at compile time. A very example of this “Divide by zero”. suppose I write a program to divide the entered number by 0. Now, this issue won’t throw any compile time error, as this issue will only come up during execution.

Exception vs Error :

Exception handling:

C# offers exception handling, to maintain your code and simplify the process of handling the exceptions. It offers 4 keywords

  • try
  • catch
  • finally
  • throw

let’s dig deeper into these 4 keywords and know more about how to use them and how they work.

To handle the exception, It is important to know the places that might cause an exception. In your code sections that might throw an exception needs to be placed under try block. Once the exception is encountered the flow is sent to catch block, where the exception is handled. An example is attached below.

In the example above you must have noticed a code section named finally, finally section is the section that will be executed always irrespective of the fact that exception did occur or not. As above, even if the exception has occurred we want to print the result.

C# gives a wide range of exceptions to work on and also a way to create custom exceptions, which we will have a look at going further.

Custom Exceptions:

c# also gives the option to create custom built or user built exceptions. user build exceptions are derived from class Exception. As mentioned above throw keyword is used to send the flow of exception handling forward. let’s understand this with an example below.

Some FAQs

Question 1: What is the difference between System exceptions and Application exceptions?

Answer 1: System exceptions are not recoverable or fatal errors generated through the system. System exceptions are derived from the base class System.SystemException which in itself is a derived class of SystemException. For example a database crash.

Application level exceptions occur when a recoverable error occurs, for example, wrong input or mathematical exceptions. It is derived from the base class System.ApplicationException which is also a derived class of SystemException.

Question 2: What happens if exception is not handled by the code?

Answer 2: CLR terminates the program execution where it encounters an exception.

Question 3: What is the difference between throw and throw ex ?

Answer 3: throw is used to throw an exception that occurred during flow, whereas throw ex is used as an outer shell to send the object for the exception occurred. throw exception will reset your stack trace so the error will appear from the line where throw ex wrote while throw does not reset stack trace and you will get information about the original exception.

Question 4: Does finally get executed if the code throws an error?

Answer 4: Yes, finally block is always executed irrespective exception did occur or not.

Question 5: Explain the hierarchy of exceptions in C#?

Answer 5: In C# all exceptions are derived from a base class called Exception. This class is further divided into 2 parts that are application exception and system exception. a detailed picture is attached above within the article.

Question 6: Is it mandatory for a piece of code to have catch or finally when try block is there?

Answer 6: No, catch and finally blocks are not mandatory. But for better handling catch block should be present to catch the exception and handle it properly.

Question 7: Can finally bock have return statement ?

Answer 7: No! finally cannot have return statements.

Question 8: Whats the syntax for catching all kinds of exception.

Answer 8:
This will catch exceptions thrown by managed as well as unmanaged code.

I hope this was beneficial to you. I tried to sum up all the knowledge I gained by reading documents, videos on internet and implementation in code. In case you have any suggestions, improvements or add on to the article please do comment below. It would come to be a great source of learning.

Thank you!

Note: It’s a tough phase that the whole world is going through. I hope everyone reading this is sitting back home, safe and sound. Social distancing is our way of killing this pandemic. We are fighting together with this situation of COVID-19 and we will get through this. We are in it together. Stay Safe! Stay Inside!

--

--

Prerna Kaushik
Prerna Kaushik

Written by Prerna Kaushik

An enthusiast and a goal digger.

Responses (1)