Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dealing with Exceptions caused by User Input | Introduction
Introduction to .NET with C#

bookDealing with Exceptions caused by User Input

Recap:

  • try-catch blocks can be used for avoiding program crashes in case of invalid inputs;
  • The code that parses user input can be enclosed into a try block, and an error message can be displayed to the user via the catch block in case the parsing fails;
  • We can enclose the try-catch block into an infinite while loop, which breaks after the user input is successfully parsed and stored. This way, we can make sure that instead of crashing, the program prompts the user for input until the user gives the correct input.

1. In a try-catch structure, where should the code that might throw an exception be placed?

2. What will happen if the user enters "abc" when prompted?

question mark

In a try-catch structure, where should the code that might throw an exception be placed?

Select the correct answer

question mark

What will happen if the user enters "abc" when prompted?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

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

bookDealing with Exceptions caused by User Input

Recap:

  • try-catch blocks can be used for avoiding program crashes in case of invalid inputs;
  • The code that parses user input can be enclosed into a try block, and an error message can be displayed to the user via the catch block in case the parsing fails;
  • We can enclose the try-catch block into an infinite while loop, which breaks after the user input is successfully parsed and stored. This way, we can make sure that instead of crashing, the program prompts the user for input until the user gives the correct input.

1. In a try-catch structure, where should the code that might throw an exception be placed?

2. What will happen if the user enters "abc" when prompted?

question mark

In a try-catch structure, where should the code that might throw an exception be placed?

Select the correct answer

question mark

What will happen if the user enters "abc" when prompted?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
some-alt