Practice 50 C# Exception Handling Programming Questions, TechnoVlogs

Practice 50 C# Exception Handling Programming Questions


Q1. Write a C# program to handle a division by zero exception.  
  Input: 10 / 0  
  Output: Error: Division by zero is not allowed.

Q2. Write a C# program to handle an array index out of bounds exception.  
  Input: Array = [1, 2, 3], Index = 5  
  Output: Error: Index out of range.

Q3. Write a C# program to handle a file not found exception when accessing a non-existent file.  
  Input: FilePath = "nonexistent.txt"  
  Output: Error: The file could not be found.

Q4. Write a C# program to handle a null reference exception when dereferencing a null object.  
  Input: Object = null  
  Output: Error: Object reference not set to an instance of an object.

Q5. Write a C# program to handle a format exception when converting a string to a number.  
  Input: String = "abc123"  
  Output: Error: Input string was not in a correct format.

Q6. Write a C# program to handle multiple exceptions in a try-catch block.  
  Input: Divide(10, 0), String = "abc123"  
  Output: Error: Division by zero is not allowed. Error: Input string was not in a correct format.

Q7. Write a C# program to demonstrate the use of a finally block.  
  Input: FileReadOperation  
  Output: File read successfully. Closing resources.

Q8. Write a C# program to handle a divide by zero exception using try-catch-finally.  
  Input: 10 / 0  
  Output:  
  Error: Division by zero is not allowed.
  Finally block executed.

Q9. Write a C# program to handle an invalid cast exception when casting an object to a different type.  
  Input: Object = "hello", CastType = int  
  Output: Error: Invalid cast from 'System.String' to 'System.Int32'.

Q10. Write a C# program to handle an invalid operation exception.  
   Input: Thread.Sleep(-1)  
   Output: Error: Specified argument was out of the range of valid values.

Q11. Write a C# program to handle an overflow exception when performing an arithmetic operation.  
   Input: int.MaxValue + 1  
   Output: Error: Arithmetic operation resulted in an overflow.

Q12. Write a C# program to handle a timeout exception.  
   Input: Task.Delay(TimeSpan.FromSeconds(1))  
   Output: Error: The operation has timed out.

Q13. Write a C# program to handle a directory not found exception.  
   Input: DirectoryPath = "nonexistent"  
   Output: Error: The directory could not be found.

Q14. Write a C# program to handle a format exception for date parsing.  
   Input: Date = "31-02-2025"  
   Output: Error: The date format is invalid.

Q15. Write a C# program to handle a stack overflow exception.  
   Input: Recursion(n)  
   Output: Error: Stack overflow.

Q16. Write a C# program to handle a key not found exception while accessing a dictionary.  
   Input: Dictionary = { "key1": "value1" }, Key = "key2"  
   Output: Error: Key not found in the dictionary.

Q17. Write a C# program to handle a file access denied exception.  
   Input: FilePath = "read-only.txt"  
   Output: Error: Access to the file is denied.

Q18. Write a C# program to demonstrate exception chaining.  
   Input: ThrowNestedException()  
   Output: Error: An error occurred. See inner exception for details.

Q19. Write a C# program to handle a system exception for an unhandled exception.  
   Input: Unhandled exception  
   Output: Error: An unhandled exception occurred.

Q20. Write a C# program to catch exceptions using exception filters.  
   Input: String = "10abc", Filter: int.TryParse()  
   Output: Error: Input string was not in a correct format.

Q21. Write a C# program to handle a network-related exception.  
   Input: ConnectToServer()  
   Output: Error: Network connection failed.

Q22. Write a C# program to handle an out-of-memory exception.  
   Input: AllocateLargeMemory()  
   Output: Error: Insufficient memory to continue the execution.

Q23. Write a C# program to demonstrate handling an exception with custom error messages.  
   Input: Divide(10, 0)  
   Output: Error: You cannot divide by zero.

Q24. Write a C# program to handle a file already exists exception when creating a file.  
   Input: FilePath = "existingFile.txt"  
   Output: Error: The file already exists.

Q25. Write a C# program to handle an unauthorized access exception when accessing a resource.  
   Input: FilePath = "protectedFile.txt"  
   Output: Error: Unauthorized access to the file.

Q26. Write a C# program to demonstrate exception handling using the "throw" keyword.  
   Input: ThrowCustomException()  
   Output: Error: A custom exception occurred.

Q27. Write a C# program to catch a custom exception.  
   Input: InvalidUserInput()  
   Output: Error: Invalid user input.

Q28. Write a C# program to catch an argument out of range exception.  
   Input: Array = [1, 2], Index = 5  
   Output: Error: Argument out of range.

Q29. Write a C# program to handle a resource not found exception when accessing a resource.  
   Input: ResourcePath = "resourceNotFound"  
   Output: Error: Resource not found.

Q30. Write a C# program to handle an invalid file format exception.  
   Input: FilePath = "file.txt", Format = pdf  
   Output: Error: Invalid file format.

Q31. Write a C# program to handle a type mismatch exception.  
   Input: String = "hello", CastType = double  
   Output: Error: Cannot cast from 'System.String' to 'System.Double'.

Q32. Write a C# program to catch and rethrow an exception.  
   Input: ThrowAndCatchException()  
   Output: Error: Exception rethrown after catch.

Q33. Write a C# program to handle a divide by zero exception and log the error.  
   Input: 10 / 0  
   Output: Error: Division by zero. Error logged.

Q34. Write a C# program to handle a catch-all exception using a generic exception.  
   Input: InvalidInput()  
   Output: Error: An unexpected error occurred.

Q35. Write a C# program to catch a NullReferenceException and display a custom message.  
   Input: Object = null  
   Output: Error: Object cannot be null.

Q36. Write a C# program to handle a file already opened exception.  
   Input: OpenFile("file.txt")  
   Output: Error: The file is already opened.

Q37. Write a C# program to handle a system exception with specific exception types.  
   Input: Thread.Sleep(-1)  
   Output: Error: Argument out of range.

Q38. Write a C# program to handle a SqlException when executing a query.  
   Input: SQL Query = "SELECT * FROM invalidTable"  
   Output: Error: SQL query execution failed.

Q39. Write a C# program to catch a FormatException when parsing a string to an integer.  
   Input: String = "notAnInteger"  
   Output: Error: Unable to parse the input.

Q40. Write a C# program to handle and display an error message for invalid user input.  
   Input: UserInput = "abc"  
   Output: Error: Invalid input. Please enter a valid number.

Q41. Write a C# program to handle a network timeout exception.  
   Input: ConnectToServer(TimeOut)  
   Output: Error: Network timeout.

Q42. Write a C# program to handle a NullPointerException when accessing a null object.  
   Input: Object = null  
   Output: Error: Object reference is null.

Q43. Write a C# program to handle an object disposed exception.  
   Input: DisposeObject()  
   Output: Error: Object has been disposed.

Q44. Write a C# program to demonstrate throwing a custom exception when invalid input is detected.  
   Input: InvalidInput()  
   Output: Error: Custom exception for invalid input.

Q45. Write a C# program to handle a file path too long exception.  
   Input: FilePath = "A very long path"  
   Output: Error: The specified file path is too long.

Q46. Write a C# program to handle an invalid cast exception during object conversion.  
   Input: Object = 5, CastType = string  
   Output: Error: Invalid cast exception.

Q47. Write a C# program to catch an out-of-range exception when accessing an element in a list.  

   Input: List = [1, 2, 3], Index = 5  
   Output: Error: Index out of range.

Q48. Write a C# program to handle an unauthorized access exception with a custom message.  
   Input: FilePath = "restricted.txt"  
   Output: Error: Unauthorized access to the file.

Q49. Write a C# program to catch a custom exception with a detailed message.  
   Input: InvalidOperation()  
   Output: Error: Operation is not valid in the current state.

Q50. Write a C# program to handle an exception and display the exception stack trace.  
   Input: Divide(10, 0)  
   Output:  
   Error: Division by zero
   Stack Trace: at Program.Main()...

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!