C# try/catch27 Aug 2025 | 7 min read In the C# programming language, exception handling is performed using a try/catch statement. The try block is utilized to write the code that might cause exceptions during execution. When an error occurs, the program immediately moves to the catch block to handle the run-time error. It enables multiple catch blocks to handle the different types of exceptions, such as DivideByZeroException, NullReferenceException, and IndexOutOfRangeException. Syntax:It has the following syntax. In this syntax,
C# Simple try/catch ExampleLet us take an example to illustrate the try/catch block in C#. ExampleCompile and RunOutput: ERROR! Error: Cannot divide by Zero. Explanation: In this example, we demonstrate the exception handling in C#. First, we have taken the two variables n1 and n2 with values 10 and 0. After that, we divide n1 by n2, which causes a DivideByZeroException. Rather than crashing the program, the catch block handles the error and shows the error message. C# Try blockIn the C# programming language, the try block is used to wrap the code that may throw an exception during the run-time of the program. The try block contains the code that may cause an error, and if an error occurs, the program immediately jumps to the catch block to handle it. Syntax: It has the following syntax. C# Catch blockIn C#, the catch block is used to handle the exceptions of the code. This block executes only when an error occurs in the try block. It can also access the exception object to get the details, such as the error message and stack trace. It allows us to prevent the program from crashing and enables us to handle errors in an effective manner. Syntax: It has the following syntax. Finally blockIn C#, the finally block is a code block that always runs whether the condition is true or not. It is always executed after the try and catch block. It is mainly utilized to perform cleanup operations, such as releasing resources, closing files, and disconnecting from a database. Syntax: It has the following syntax. C# Simple Example of handling all exceptions:Let us take an example of handling all exceptions in the try catch block. ExampleCompile and RunOutput: Error: Division by zero is not allowed Finally block executed: Cleaning up resources or finishing tasks. Explanation: In this example, we have taken a class Division, and declared two floating point numbers A and B, where A is 75 and B is 0. After that, the try block checks for division by zero and throws an exception if the divisor is 0. The catch block handles the exception and prints an error message. The finally block always executes, which ensures clean-up or final tasks are performed regardless of whether an exception occurred. Multiple Catch blockIn C#, the multiple catch block provides a powerful mechanism to handle the different types of exceptions that may occur at runtime. A single try block can be followed by several catch blocks, and every block is used to handle a specific exception type. It enables us to respond to different types of error conditions in the program according to the requirements. Syntax: It has the following syntax. C# Simple Example Multiple Catch blockLet us take an example to illustrate the multiple catch blocks in C#. ExampleCompile and RunOutput: It has the following output. Case 1: Enter any number for your choice: 2 The Value at index 2 = 35 The Program finished. Case 2: Please input a number for your choice 5 The index you entered is invalid. The Program finished. Case 3: Please input a number for your choice abc Please enter a valid number The Program finished. Explanation: In this example, we have taken an integer type array and assigned the values. After that, we take a number from the user and use it as an index to display a value from the array. If the index value is invalid, it shows an error message. If the input is not a number, it gives another error, and any other errors are also handled. In the end, it always prints that the program has finished. C# Nested try/catch blockIn the C# programming language, a nested try/catch block refers to placing one try/catch block inside another to handle errors at multiple levels. The inner try/catch handles the error that happens in the part of the code. If it does not catch an exception, the outer try/catch block can handle errors in the runtime environment. Syntax: It has the following syntax. C# Nested try/catch ExampleLet us take an example to illustrate the nested try/catch in C#. ExampleCompile and RunOutput: The Outer try block starts. The Inner try block starts. Index out of range. The Finally block executed. Explanation: In this example, we demonstrate the nested try/catch block along with a finally block in C#. Inside the inner try block, it tries to access numbers[5], which does not exist and throws an IndexOutOfException. The outer catch block deals with the IndexOutOfRangeException and shows the corresponding error message. After that, when exceptions are handled, the finally block executes, which always runs and shows the message "Finally block executed". ConclusionIn conclusion, the try/catch block is an important feature to handle run-time errors without crashing the program. Using the try, catch, and finally blocks makes the code more reliable and prevents the crashing of the program. It enables multiple catch blocks to handle the different types of exceptions. C# try/catch FAQs1) Describe a try/catch block in C#? In the C# programming language, exception handling is performed using a try/catch statement. The try block is used to write the code that might cause exceptions. When an error occurs, the program jumps to the catch block to handle the run-time error. It enables multiple catch blocks to handle the different types of exceptions. 2) Are multiple catch blocks allowed with a single try block? Yes, we have used multiple catch blocks in a single try block in C#. 3) Is the finally block mandatory in a try/catch block in C#? No, the finally block is not mandatory in a try/catch block. It is the optional block in exception handling. We use it when we want some code to run always, whether exceptions occur or not. 4) Define multiple catch blocks in C#? In C#, the multiple catch block provides a powerful mechanism for handling the different types of exceptions to handle the run-time error. Since the multiple exceptions are handled using multiple catch blocks. 5) What happens if any catch blocks match the exceptions? If no catch blocks match the exception type, the program will terminate, and the unhandled exceptions will be thrown to the runtime environment. Next TopicC# finally |
We request you to subscribe our newsletter for upcoming updates.