C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, exceptions are runtime errors that occur during the execution of a program. Handling exceptions is essential for creating robust and fault-tolerant applications. In this tutorial, we'll cover the basics of handling exceptions in C# using try-catch-finally blocks and creating custom exception classes.
To handle exceptions, you can use a try-catch-finally
block. The try
block contains the code that might throw an exception, the catch
block contains the code to handle the exception, and the optional finally
block contains code that is always executed, whether an exception is thrown or not.
Here's an example demonstrating how to use a try-catch-finally block:
using System; namespace ExceptionTutorial { class Program { static void Main(string[] args) { int[] numbers = new int[3]; try { Console.WriteLine("Trying to access an invalid index..."); int value = numbers[5]; // Throws an IndexOutOfRangeException } catch (IndexOutOfRangeException ex) { Console.WriteLine("An exception occurred: " + ex.Message); } finally { Console.WriteLine("Finally block executed."); } } } }
In this example, we try to access an invalid index in an array, which will throw an IndexOutOfRangeException
. The catch
block will handle the exception and display a message. The finally
block will be executed regardless of whether an exception was thrown or not.
You can catch multiple exceptions by using multiple catch blocks, each handling a specific exception type:
try { // Code that might throw an exception } catch (ArgumentNullException ex) { // Handle ArgumentNullException } catch (ArgumentException ex) { // Handle ArgumentException } catch (Exception ex) { // Handle all other exceptions } finally { // Code that is always executed }
To create a custom exception class, inherit from the System.Exception
class or one of its derived classes and add any custom properties or methods you need:
public class CustomException : Exception { public CustomException() { } public CustomException(string message) : base(message) { } public CustomException(string message, Exception inner) : base(message, inner) { } }
You can then throw and catch your custom exception just like any other exception:
try { throw new CustomException("This is a custom exception!"); } catch (CustomException ex) { Console.WriteLine("Caught a custom exception: " + ex.Message); }
In this tutorial, we've covered the basics of handling exceptions in C# using try-catch-finally blocks and creating custom exception classes. Properly handling exceptions helps create robust and fault-tolerant applications that can handle unexpected situations gracefully.
Try-catch block in C#:
using System; class Program { static void Main() { try { // Code that may cause an exception int result = 10 / 0; // This will throw a DivideByZeroException } catch (Exception ex) { // Handle the exception Console.WriteLine($"Exception caught: {ex.Message}"); } } }
C# catch multiple exceptions:
using System; class Program { static void Main() { try { // Code that may cause exceptions int[] numbers = { 1, 2, 3 }; Console.WriteLine(numbers[5]); // This will throw an IndexOutOfRangeException } catch (IndexOutOfRangeException ex) { Console.WriteLine($"Index out of range: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"General exception caught: {ex.Message}"); } } }
Custom exception handling in C#:
using System; // Custom exception class public class CustomException : Exception { public CustomException(string message) : base(message) { } } class Program { static void Main() { try { throw new CustomException("Custom exception occurred"); } catch (CustomException ex) { Console.WriteLine($"Custom exception caught: {ex.Message}"); } } }
Exception filters in C#:
using System; class Program { static void Main() { try { int result = Divide(10, 0); } catch (Exception ex) when (ex is DivideByZeroException) { Console.WriteLine($"Divide by zero exception caught: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"General exception caught: {ex.Message}"); } } static int Divide(int a, int b) { if (b == 0) { throw new DivideByZeroException("Cannot divide by zero."); } return a / b; } }