Advertisement



< Prev
Next >



C# Exception Handling





What is an Exception in C#?


In C# language, Exception is an error which occurs only during the runtime of a program.

When an exception occurs during the execution of a program, it is said to be thrown by the C# runtime system, which is known as Common Language Runtime(CLR).

If this thrown exception is not handled, it stops the normal execution of the program and it ends abruptly. That's why we should handle an exception when it is thrown by the program. The process of handling an exception is called Exception Handling.




Some situations due to which an Exception is thrown by a program:






What happens when an Exception is thrown?


Exceptions are thrown at the runtime of a program. If an exception, which is thrown during the runtime of a program, is not handled, the program terminates right away without executing the remaining statements. Let us understand this by an example here.
Looking at the diagram above, it is easy to understand that if an exception is thrown when statement2 is executed, the program ends abruptly without executing any statement after it. Hence, statement3 and statement4 will not be executed as a result of an exception at statement2.



Advertisement




Each exception has a type -:


In C#, there are many types of exceptions, where each exception type is represented by a class. A built-in class known as Exception, which is defined in the System namespace, is the base class for all exception classes i.e. all the exception classes are derived from it.

One of the most important subclasses of Exception class is SystemException class, from which all the exceptions thrown by C# runtime system(CLR) are derived.

In the table below, we have given a short preview about some important exceptions in C#.

Exception Class Description
Exception
This is a base class for all built-in exceptions, it represents errors that occur during application execution.

SystemException
This is a base class from which all the exceptions thrown by C# runtime system(CLR) are derived.

ArithmeticException

This exception is thrown for errors in an arithmetic, casting, or conversion operation.

ArrayTypeMismatchException
The exception that is thrown when an attempt is made to store an element of the wrong type within an array.

ArgumentNullException
This type of exception is raised when result of an arithmetic expression is too large to be displayed.

DivideByZeroException
This type of exception is thrown an argument that is passed to a method is null.

PathTooLongException
This type of exception is raised when there is a syntax error in Python's script/program.

DirectoryNotFoundException
The exception that is thrown when the path of a directory path is not valid.

InvalidDataException
The exception that is thrown when invalid data is detected in the data stream.

IOException Class
This type of exception is thrown during an I/O error, while accessing information using streams, files and directories. .

UnauthorizedAccessException
The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.

OutOfMemoryException
This type of exception is thrown when there is not enough memory to execute a program.

InvalidTimeZoneException
The exception that is thrown when the specified time zone information is invalid.
MemberAccessException
The exception that is thrown when an attempt to access a class member fails.

NullReferenceException
This type of exception is thrown when we are trying to operate on a null reference i.e. a reference which does not refer to an object.

InvalidCastException
The exception that is thrown for invalid casting or explicit conversion.

IndexOutOfRangeException
This type of exception is thrown when we try to access an element of an array with an out-of-range index value.

KeyNotFoundException
This type of exception is raised when a local or global name is not found or its value is not defined.

UriFormatException
This type of exception is thrown when an invalid Uniform Resource Locator(URI) is used.

TimeoutException
This type of exception is thrown when the time allocated for an operation has passed.



In the next few articles, we will discuss some very important keywords which are used to handle exceptions, such as try, catch, finally and throw.



Please share this article -





< Prev
Next >
< C# String Join() Method
C#Exception propagation >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement