w3resource

C# Exception Handling: Exercises, Practice, Solutions

C# Sharp Exception Handling [ 13 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a C# program that prompts the user to input two numbers and divides them. Handle an exception when the user enters non-numeric values.

Click me to see the solution

2. Write a C# program to implement a method that takes an integer as input and throws an exception if the number is negative. Handle the exception in the calling code.

Click me to see the solution

3. Write a C# program that reads a file path from the user and tries to open the file. Handle exceptions if the file does not exist.

Click me to see the solution

4. Write a C# program that prompts the user to input a numeric integer and throws an exception if the number is less than 0 or greater than 1000.

Click me to see the solution

5. Write a C# program that implements a method that takes an array of integers as input and calculates the average value. Handle the exception if the array is empty.

Click me to see the solution

6. Write a C# program that reads a string from the user and converts it to an integer. Handle the exception if the input cannot be parsed into an integer.

Click me to see the solution

7. Write a C# program that reads a list of integers from the user. Handle the exception that occurs if the user enters a value outside the range of Int32.

Click me to see the solution

8. Write a C# program that implements a method that divides two numbers. Handle the DivideByZeroException that occurs if the denominator is 0.

Click me to see the solution

9. Write a C# program that creates a method that reads a date from the user in the format "dd/mm/yyyy" and converts it to a DateTime object. Handle an exception if the input format is invalid.

Click me to see the solution

10. Write a C# program that reads a number from the user and calculates its square root. Handle the exception if the number is negative.

Click me to see the solution

11. Write a C# program that creates a method that takes a string as input and converts it to uppercase. Handle the NullReferenceException that occurs if the input string is null.

Click me to see the solution

12. Write a C# program that creates a method that calculates the factorial of a given number. Handle the OverflowException that occurs if the result exceeds the Int32 maximum value.

Click me to see the solution

13. Write a C# program that reads a list of URLs from the user and downloads the content of each URL. Handle the exception if any URL is inaccessible.

Click me to see the solution

C# Sharp Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/exception-handling/index.php