MCQs on Exception Handling | C#

This set of 30 multiple-choice questions (MCQs) will help you master exception handling in C#, covering exception classes, handling multiple exceptions, creating custom exceptions, and filtering or rethrowing exceptions effectively.


Chapter 12: Exception Handling in C# – MCQs

1. Exception Classes and Types

  1. Which class is the base class for all exceptions in C#?
    • a) System.Exception
    • b) System.Object
    • c) System.BaseException
    • d) System.Error
  2. Which of the following is an example of a predefined exception in C#?
    • a) System.Exception
    • b) System.NullReferenceException
    • c) CustomException
    • d) ApplicationError
  3. What type of exception is thrown when a null object reference is accessed in C#?
    • a) IndexOutOfRangeException
    • b) ArgumentException
    • c) NullReferenceException
    • d) FileNotFoundException
  4. Which exception type is thrown when a method receives an argument that is outside the acceptable range?
    • a) NullReferenceException
    • b) IndexOutOfRangeException
    • c) ArgumentOutOfRangeException
    • d) ArgumentNullException
  5. What is the main purpose of exception classes in C#?
    • a) To allow error handling in the application.
    • b) To display an error message to the user.
    • c) To terminate the application immediately.
    • d) To hide the errors from the user.
  6. Which of the following is a type of exception in C# that is thrown when an invalid operation is performed, such as dividing by zero?
    • a) ArgumentException
    • b) DivideByZeroException
    • c) OverflowException
    • d) NullReferenceException
  7. How does C# distinguish between different types of exceptions?
    • a) By the error code
    • b) By the exception class type
    • c) By the exception message
    • d) By the stack trace

2. Handling Multiple Exceptions

  1. How can you handle multiple exceptions in a single try-catch block in C#?
    • a) By using multiple catch blocks for each exception type.
    • b) By using a single catch block with multiple exception types.
    • c) By using the finally block.
    • d) By throwing the exception to the caller.
  2. What happens when multiple catch blocks match the exception type in C#?
    • a) The first matching catch block is executed.
    • b) All matching catch blocks are executed.
    • c) The last matching catch block is executed.
    • d) An error is thrown at runtime.
  3. What type of exception is handled by the following code?
    try { // Code that throws exception } catch (NullReferenceException ex) { // Handle NullReferenceException } catch (Exception ex) { // Handle general exceptions }
    • a) NullReferenceException
    • b) ArgumentException
    • c) Exception
    • d) IndexOutOfRangeException
  4. What is the main advantage of using multiple catch blocks in C# exception handling?
    • a) To handle different types of exceptions separately.
    • b) To speed up exception processing.
    • c) To avoid throwing exceptions.
    • d) To prevent application crashes.
  5. Can you catch multiple exception types in a single catch block in C#?
    • a) Yes, by separating exception types with commas.
    • b) No, each exception type must be caught separately.
    • c) Yes, by using try-catch-finally structure.
    • d) Yes, by using a wildcard character.
  6. Which of the following is a correct way to catch multiple exceptions in a single catch block?
    • a) catch (NullReferenceException | ArgumentNullException ex)
    • b) catch (Exception ex) { }
    • c) catch (Exception ex, ArgumentException ex2)
    • d) catch (Exception | NullReferenceException)
  7. What is the use of the when keyword in C# exception handling?
    • a) To filter exceptions based on a condition.
    • b) To trigger custom exceptions.
    • c) To skip the exception handling.
    • d) To log exceptions.

3. Creating Custom Exceptions

  1. How do you create a custom exception class in C#?
    • a) By inheriting from System.Exception.
    • b) By inheriting from System.Object.
    • c) By creating a static class.
    • d) By using the throw keyword.
  2. Which constructor is usually added to custom exceptions in C#?
    • a) A constructor with no parameters.
    • b) A constructor with parameters for the exception message and inner exception.
    • c) A constructor with parameters for the exception type and stack trace.
    • d) A constructor with a default message only.
  3. How do you throw a custom exception in C#?
    • a) throw new CustomException();
    • b) throw CustomException;
    • c) CustomException();
    • d) throw new Exception(CustomException);
  4. When creating a custom exception class, which property should be inherited from the base exception class?
    • a) Message
    • b) StackTrace
    • c) InnerException
    • d) All of the above
  5. What is the recommended practice for naming custom exception classes in C#?
    • a) Use a name that clearly describes the exception type.
    • b) Use only one word for the class name.
    • c) Use a name that ends with Exception.
    • d) Use abbreviations for clarity.
  6. Which of the following is a valid custom exception class declaration in C#?
    • a) public class MyCustomException : Exception { }
    • b) class MyCustomException extends Exception { }
    • c) public class MyCustomException { }
    • d) public MyCustomException() : Exception { }
  7. Why should custom exceptions typically include a constructor with parameters for message and inner exception?
    • a) To allow detailed error messages and preserve stack trace information.
    • b) To ensure backward compatibility with older code.
    • c) To avoid using the throw keyword.
    • d) To allow automated exception handling by the framework.

4. Exception Filtering and Rethrowing Exceptions

  1. What does exception filtering allow you to do in C#?
    • a) It allows you to specify conditions for catching an exception.
    • b) It forces the exception to be thrown in a specific block.
    • c) It automatically handles any exception without any checks.
    • d) It helps you debug exceptions.
  2. Which of the following code shows how to filter exceptions using when in C#?
    • a) catch (Exception ex) when (ex.Message.Contains("error")) { }
    • b) catch (Exception ex) { if (ex.Message.Contains("error")) { } }
    • c) catch (Exception ex) { } when (ex.Message.Contains("error"))
    • d) catch (Exception ex when ex.Message.Contains("error")) { }
  3. What is the primary use of the throw keyword in exception handling?
    • a) To throw a new exception.
    • b) To filter exceptions.
    • c) To catch exceptions.
    • d) To create custom exceptions.
  4. How do you rethrow an exception in C# after it is caught?
    • a) throw;
    • b) throw ex;
    • c) rethrow ex;
    • d) continue;
  5. What happens when you rethrow an exception in C# without modifying it?
    • a) The original exception is passed to the outer catch block.
    • b) The exception is logged automatically.
    • c) The program terminates immediately.
    • d) A new exception is created.
  6. Can you rethrow an exception caught in a catch block in C#?
    • a) Yes, using the throw; statement.
    • b) No, exceptions cannot be rethrown.
    • c) Yes, but you must use throw new Exception();.
    • d) Yes, but only if the exception is custom.
  7. What is the difference between throw and throw ex; in C#?
    • a) throw preserves the original stack trace, while throw ex; does not.
    • b) throw ex; preserves the stack trace, while throw does not.
    • c) There is no difference; both behave the same.
    • d) throw is used for custom exceptions, and throw ex; for built-in exceptions.
  8. Which of the following is the correct order of exception handling?
    • a) try block, catch block, finally block
    • b) catch block, finally block, try block
    • c) finally block, catch block, try block
    • d) try block, finally block, catch block
  9. Which exception filter expression in C# will catch only exceptions of type ArgumentException with a specific message?
    • a) catch (ArgumentException ex) when (ex.Message.Contains("invalid")) { }
    • b) catch (ArgumentException ex) { if (ex.Message.Contains("invalid")) { } }
    • c) catch (ArgumentException ex) { } when (ex.Message.Contains("invalid"))
    • d) catch (ArgumentException ex) { ex.Message.Contains("invalid"); }

Answer Key

QnoAnswer
1a) System.Exception
2b) System.NullReferenceException
3c) NullReferenceException
4c) ArgumentOutOfRangeException
5a) To allow error handling in the application.
6b) DivideByZeroException
7b) By the exception class type
8b) By using a single catch block with multiple exception types.
9a) The first matching catch block is executed.
10c) Exception
11a) To handle different types of exceptions separately.
12a) Yes, by separating exception types with commas.
13a) `catch (NullReferenceException
14a) To filter exceptions based on a condition.
15a) By inheriting from System.Exception.
16b) A constructor with parameters for the exception message and inner exception.
17a) throw new CustomException();
18d) All of the above
19a) Use a name that clearly describes the exception type.
20a) public class MyCustomException : Exception { }
21a) To allow detailed error messages and preserve stack trace information.
22a) It allows you to specify conditions for catching an exception.
23a) catch (Exception ex) when (ex.Message.Contains("error")) { }
24a) To throw a new exception.
25a) throw;
26a) The original exception is passed to the outer catch block.
27a) Yes, using the throw; statement.
28a) throw preserves the original stack trace, while throw ex; does not.
29a) try block, catch block, finally block
30a) catch (ArgumentException ex) when (ex.Message.Contains("invalid")) { }

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top