What Are Exceptions?
When you write C# programs, you sometimes encounter unexpected problems during execution. These problems are called exceptions. An exception is a special object that signals an error or unusual situation that disrupts the normal flow of your program. Unlike regular errors, which might simply crash your program or go unnoticed, exceptions provide a structured way to detect, report, and handle issues as they happen. This helps you write safer, more reliable code by allowing you to respond to problems—such as invalid user input, missing files, or network failures—without causing your entire application to fail unexpectedly.
Program.cs
1234567891011121314151617181920212223using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { try { int numerator = 10; int denominator = 0; int result = numerator / denominator; Console.WriteLine("Result: " + result); } catch (DivideByZeroException ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } }
In the previous code, the try block contains code that might cause an exception. Here, dividing by zero triggers a DivideByZeroException. When this happens, the flow of execution immediately jumps to the matching catch block. The catch block receives the exception object, allowing you to react—such as by displaying an error message—instead of letting the program crash. After the catch block finishes, the program continues running if there is more code after the try-catch structure. This approach lets you handle errors gracefully and keep your program stable.
Program.cs
123456789101112131415161718192021222324252627282930313233using System; namespace ConsoleApp { // This code demonstrates multiple catch blocks for different exception types. public class Program { public static void Main(string[] args) { try { string input = null; int number = int.Parse(input); // This will throw an exception. } catch (FormatException ex) { // Handles cases where input is not a valid number format. Console.WriteLine("Input was not in a correct format." + ex.Message); } catch (ArgumentNullException ex) { // Handles cases where input is null. Console.WriteLine("Input cannot be null." + ex.Message); } catch (Exception ex) { // Handles any other exceptions not previously caught. Console.WriteLine("An unexpected error occurred." + ex.Message); } } } }
You will often find exceptions helpful in real-world situations where things can go wrong outside your program's direct control. For instance:
- When trying to open a file that might not exist;
- When connecting to a network resource that could be unavailable;
- When converting user input that may be invalid.
Exceptions give you a way to handle these unpredictable events. By catching and responding to exceptions, you can provide clear feedback to users and keep your application running smoothly.
1. What is the primary purpose of exceptions in C#?
2. Which block is used to handle exceptions in C#?
3. Why should you avoid catching System.Exception unless necessary?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 4.17
What Are Exceptions?
Veeg om het menu te tonen
When you write C# programs, you sometimes encounter unexpected problems during execution. These problems are called exceptions. An exception is a special object that signals an error or unusual situation that disrupts the normal flow of your program. Unlike regular errors, which might simply crash your program or go unnoticed, exceptions provide a structured way to detect, report, and handle issues as they happen. This helps you write safer, more reliable code by allowing you to respond to problems—such as invalid user input, missing files, or network failures—without causing your entire application to fail unexpectedly.
Program.cs
1234567891011121314151617181920212223using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { try { int numerator = 10; int denominator = 0; int result = numerator / denominator; Console.WriteLine("Result: " + result); } catch (DivideByZeroException ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } }
In the previous code, the try block contains code that might cause an exception. Here, dividing by zero triggers a DivideByZeroException. When this happens, the flow of execution immediately jumps to the matching catch block. The catch block receives the exception object, allowing you to react—such as by displaying an error message—instead of letting the program crash. After the catch block finishes, the program continues running if there is more code after the try-catch structure. This approach lets you handle errors gracefully and keep your program stable.
Program.cs
123456789101112131415161718192021222324252627282930313233using System; namespace ConsoleApp { // This code demonstrates multiple catch blocks for different exception types. public class Program { public static void Main(string[] args) { try { string input = null; int number = int.Parse(input); // This will throw an exception. } catch (FormatException ex) { // Handles cases where input is not a valid number format. Console.WriteLine("Input was not in a correct format." + ex.Message); } catch (ArgumentNullException ex) { // Handles cases where input is null. Console.WriteLine("Input cannot be null." + ex.Message); } catch (Exception ex) { // Handles any other exceptions not previously caught. Console.WriteLine("An unexpected error occurred." + ex.Message); } } } }
You will often find exceptions helpful in real-world situations where things can go wrong outside your program's direct control. For instance:
- When trying to open a file that might not exist;
- When connecting to a network resource that could be unavailable;
- When converting user input that may be invalid.
Exceptions give you a way to handle these unpredictable events. By catching and responding to exceptions, you can provide clear feedback to users and keep your application running smoothly.
1. What is the primary purpose of exceptions in C#?
2. Which block is used to handle exceptions in C#?
3. Why should you avoid catching System.Exception unless necessary?
Bedankt voor je feedback!