Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Practicing Error Handling | Additional Structures & File Handling
C# Beyond Basics

Practicing Error HandlingPracticing Error Handling

The try-catch syntax has an additional syntax which allows us to catch specific types of errors and deal with them separately:

cs

index.cs

The type Exception we used in the previous chapter simply catches all kinds of errors however there are some subtypes of Exception which catch more specific kinds of errors. Following are some common exception types:

  1. DivideByZeroException: There's a division by zero.
  2. FileNotFoundException: The file we're accessing doesn't exist.
  3. KeyNotFoundException: The dictionary key doesn't exist.
  4. IndexOutOfRangeException: The specified index of an array or list is invalid.

The term errorVarName is a variable which stores the Exception object, and has information like the error message which can be accessed through errorVarName.Message. We can omit that in case we're not using it:

cs

index.cs

Here's an example of using this kind of try-catch block:

cs

index.cs

Now using these concepts. Fill in the blanks with relevant exception types in the following code to complete the challenge.

cs

index.cs

1. Use the relevant exception type for each catch block. Read the code and understand which catch block is most appropriate for handling a certain type of exception.

using System;
using System.Collections.Generic;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 5, 7, 9 };
        var numberNames = new Dictionary<int, string>();
        
        numberNames.Add(1, "One");
        numberNames.Add(2, "Two");
        numberNames.Add(5, "Five");
        numberNames.Add(9, "Nine");
        
        int i = 0;
        while (true)
        {
            try
            {
                int key = numbers[i];
                Console.WriteLine($"{key} is {numberNames[key]}");
                i++;
            }
            catch (IndexOutOfRangeException)
            {
                break;
            }
            catch (KeyNotFoundException)
            {
                numberNames.Add(7, "Seven");
            }
        }
    }
} 
      

Все було зрозуміло?

Секція 1. Розділ 10
course content

Зміст курсу

C# Beyond Basics

Practicing Error HandlingPracticing Error Handling

The try-catch syntax has an additional syntax which allows us to catch specific types of errors and deal with them separately:

cs

index.cs

The type Exception we used in the previous chapter simply catches all kinds of errors however there are some subtypes of Exception which catch more specific kinds of errors. Following are some common exception types:

  1. DivideByZeroException: There's a division by zero.
  2. FileNotFoundException: The file we're accessing doesn't exist.
  3. KeyNotFoundException: The dictionary key doesn't exist.
  4. IndexOutOfRangeException: The specified index of an array or list is invalid.

The term errorVarName is a variable which stores the Exception object, and has information like the error message which can be accessed through errorVarName.Message. We can omit that in case we're not using it:

cs

index.cs

Here's an example of using this kind of try-catch block:

cs

index.cs

Now using these concepts. Fill in the blanks with relevant exception types in the following code to complete the challenge.

cs

index.cs

1. Use the relevant exception type for each catch block. Read the code and understand which catch block is most appropriate for handling a certain type of exception.

using System;
using System.Collections.Generic;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 5, 7, 9 };
        var numberNames = new Dictionary<int, string>();
        
        numberNames.Add(1, "One");
        numberNames.Add(2, "Two");
        numberNames.Add(5, "Five");
        numberNames.Add(9, "Nine");
        
        int i = 0;
        while (true)
        {
            try
            {
                int key = numbers[i];
                Console.WriteLine($"{key} is {numberNames[key]}");
                i++;
            }
            catch (IndexOutOfRangeException)
            {
                break;
            }
            catch (KeyNotFoundException)
            {
                numberNames.Add(7, "Seven");
            }
        }
    }
} 
      

Все було зрозуміло?

Секція 1. Розділ 10
some-alt