Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Error Handling | Additional Structures & File Handling
course content

Зміст курсу

C# Beyond Basics

Error HandlingError Handling

You might have come across the term runtime error in previous chapters. A runtime error is an error which occurs while the program is running - hence the name "runtime" error.

Usually when a runtime error occurs, the program crashes or stops responding.

The compiler can help us by pointing out most of the errors in our code but runtime errors are mostly unpredictable and often depend on uncertain parameters.

For-example, in case the file path passed into StreamReader is invalid or does not exist, it will give a runtime error and the program will crash. Therefore, we often put such dangerous code into try-catch blocks to try some code, and in case it fails, we catch and deal with the error instead of causing the program to crash.

Following is the syntax of the try-catch block:

cs

index.cs

Here Exception is a keyword which represents the datatype Exception.

Example:

cs

index.cs

We can omit the (Exception error) part from the catch statement if we're not using error.

Following are some common cases where a runtime error can occur:

Division By Zero

cs

index.cs

Invalid Index of an Array or a List

cs

index.cs

Key Not Found (for Dictionaries):

cs

index.cs

The "finally" Block

There's also an option code block called finally which is executed after the catch block is executed:

cs

index.cs

1. What will be the output of the following program?
2. In C#, what is the purpose of the `finally` block?
3. Which of the following statements is true regarding the `try-catch` block in C#?

What will be the output of the following program?

Виберіть правильну відповідь

In C#, what is the purpose of the finally block?

Виберіть правильну відповідь

question-icon

Which of the following statements is true regarding the try-catch block in C#?

Виберіть кілька правильних відповідей

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

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

Зміст курсу

C# Beyond Basics

Error HandlingError Handling

You might have come across the term runtime error in previous chapters. A runtime error is an error which occurs while the program is running - hence the name "runtime" error.

Usually when a runtime error occurs, the program crashes or stops responding.

The compiler can help us by pointing out most of the errors in our code but runtime errors are mostly unpredictable and often depend on uncertain parameters.

For-example, in case the file path passed into StreamReader is invalid or does not exist, it will give a runtime error and the program will crash. Therefore, we often put such dangerous code into try-catch blocks to try some code, and in case it fails, we catch and deal with the error instead of causing the program to crash.

Following is the syntax of the try-catch block:

cs

index.cs

Here Exception is a keyword which represents the datatype Exception.

Example:

cs

index.cs

We can omit the (Exception error) part from the catch statement if we're not using error.

Following are some common cases where a runtime error can occur:

Division By Zero

cs

index.cs

Invalid Index of an Array or a List

cs

index.cs

Key Not Found (for Dictionaries):

cs

index.cs

The "finally" Block

There's also an option code block called finally which is executed after the catch block is executed:

cs

index.cs

1. What will be the output of the following program?
2. In C#, what is the purpose of the `finally` block?
3. Which of the following statements is true regarding the `try-catch` block in C#?

What will be the output of the following program?

Виберіть правильну відповідь

In C#, what is the purpose of the finally block?

Виберіть правильну відповідь

question-icon

Which of the following statements is true regarding the try-catch block in C#?

Виберіть кілька правильних відповідей

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

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