Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære What Are Exceptions? | Fundamentals of Exception Handling
C# Exceptions and Error Handling Practice

bookWhat Are Exceptions?

Prerequisites
Forudsætninger

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you give an example of how to use try-catch in C#?

What are some common exceptions in C# and how do I handle them?

How can I create my own custom exception in C#?

bookWhat Are Exceptions?

Stryg for at vise menuen

Prerequisites
Forudsætninger

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
some-alt