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

bookChallenge: Error Handling

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

index.cs

index.cs

copy
1234567891011121314151617
try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }

The type Exception you 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. You can omit that in case we're not using it:

index.cs

index.cs

copy
12345678910111213
try { // code } catch (ExceptionType) { // code } ... finally { // code }

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

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; class Program { static void Main(string[] args) { int[] myArray = { 0, 2, 4, 6, 8, 10 }; int i = 0; while (true) { try { Console.Write(myArray[i] / i + " "); i++; } catch(DivideByZeroException) { i++; } catch(IndexOutOfRangeException) { break; } } } }

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

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Collections.Generic; class Program { 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 (___) { break; } catch (___) { numberNames.Add(7, "Seven"); } } } }

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.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
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"); } } } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 10

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What are the correct exception types to fill in the blanks?

Can you explain why each exception type is used in this context?

Can you give more examples of specific exception types?

Awesome!

Completion rate improved to 2.04

bookChallenge: Error Handling

Swipe to show menu

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

index.cs

index.cs

copy
1234567891011121314151617
try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }

The type Exception you 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. You can omit that in case we're not using it:

index.cs

index.cs

copy
12345678910111213
try { // code } catch (ExceptionType) { // code } ... finally { // code }

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

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using System; class Program { static void Main(string[] args) { int[] myArray = { 0, 2, 4, 6, 8, 10 }; int i = 0; while (true) { try { Console.Write(myArray[i] / i + " "); i++; } catch(DivideByZeroException) { i++; } catch(IndexOutOfRangeException) { break; } } } }

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

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using System; using System.Collections.Generic; class Program { 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 (___) { break; } catch (___) { numberNames.Add(7, "Seven"); } } } }

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.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
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"); } } } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 10
some-alt