advertisement
Exceptions in C#
Exceptions in programming are abnormal events that prevent a certain task from being completed successfully. For example, consider a vehicle that halts abruptly due to some problem in the engine. Now, until this problem is sorted out, the vehicle may not move ahead. Similarly, in C#, exceptions disrupt the normal flow of the program. Exception handling is a process of handling run-time errors. In C#, developers can handle exceptions by using try-catch or try-catch-finally constructs. In addition, C# supports custom exceptions that allow customizing the error-handling process.
Throwing and Catching Exceptions
Consider a group of boys playing throwball wherein one boy throws a ball and the other boys catch the ball. If any of the boys fail to catch the ball, the game will be terminated. Thus, the game goes on till the boys are successful in catching the ball on time.
Similarly, in C#, exceptions that occur while working on a particular program must be caught by exception handlers. If the program does not contain the appropriate exception handler, then the program might be terminated.
Catching Exceptions
Exception handling is implemented using the try-catch construct in C#. This construct consists of two blocks, the try block and the catch block. The try block encloses statements that might generate exceptions. When these exceptions are thrown, the required actions are performed using the catch block. Thus, the catch block consists of the appropriate error handlers that handle exceptions. The catch block follows the try block and may or may not contain parameters. If the catch block does not contain any parameter, it can catch any type of exception. If the catch block contains a parameter, It catches the type of exception specified by the parameter.
General Catch Block
The catch block can handle all types of exceptions. However, the type of exception that the catch block handles depends on the specified exception class. Sometimes, a developer might not know the type of exception the program might throw. In such a case, the developer can create a catch block with the base class Exception. Such catch blocks are referred to as general catch blocks.
A general catch block can handle all types of exceptions. However, the disadvantage of the general catch block is that there is no instance of the exception and thus, developers cannot know what appropriate action must be performed for handling the exception.
throw Expressions
C# 7.0 onwards has a new enhancement concerning exceptions, namely throw expressions. In earlier versions, the throw was a statement, and hence, code constructs such as conditional expressions, null coalescing expressions, and certain lambda expressions were not allowed to throw exceptions. However, this restriction does not apply to C# 7.0. Developers can now throw exceptions in all such code constructs.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0