Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen What Are Exceptions? | Fundamentals of Exception Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Exceptions and Error Handling Practice

bookWhat Are Exceptions?

Prerequisites
Voraussetzungen

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

Program.cs

copy
1234567891011121314151617181920212223
using 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

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using 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?

question mark

What is the primary purpose of exceptions in C#?

Select the correct answer

question mark

Which block is used to handle exceptions in C#?

Select the correct answer

question mark

Why should you avoid catching System.Exception unless necessary?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookWhat Are Exceptions?

Swipe um das Menü anzuzeigen

Prerequisites
Voraussetzungen

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

Program.cs

copy
1234567891011121314151617181920212223
using 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

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using 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?

question mark

What is the primary purpose of exceptions in C#?

Select the correct answer

question mark

Which block is used to handle exceptions in C#?

Select the correct answer

question mark

Why should you avoid catching System.Exception unless necessary?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 1
some-alt