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
Which class is the base class for all exceptions in C#?
a) System.Exception
b) System.Object
c) System.BaseException
d) System.Error
Which of the following is an example of a predefined exception in C#?
a) System.Exception
b) System.NullReferenceException
c) CustomException
d) ApplicationError
What type of exception is thrown when a null object reference is accessed in C#?
a) IndexOutOfRangeException
b) ArgumentException
c) NullReferenceException
d) FileNotFoundException
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
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.
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
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
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.
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.
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
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.
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.
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)
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
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.
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.
How do you throw a custom exception in C#?
a) throw new CustomException();
b) throw CustomException;
c) CustomException();
d) throw new Exception(CustomException);
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
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.
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 { }
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
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.
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")) { }
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.
How do you rethrow an exception in C# after it is caught?
a) throw;
b) throw ex;
c) rethrow ex;
d) continue;
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.
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.
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.
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
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"))