Exception Handling Questions with Answers

10 practice questions on Exception Handling (C#) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Medium

Exceptions can be thrown even from a constructor, whereas error codes cannot be returned from a constructor.

  1. True
  2. False

Answer: A. True

Explanation

Yeah its true cause constructor can not return error code.By the strict definition of 'return' then no. CTors can't return values. However they can have ref or out parameters so in the wider sense they can pass an error code back to the caller:

classwibble
{
publicwibble(out interrorCode)
{

Step-by-step solution

  1. Exceptions can be thrown from a constructor, so a constructor can report a failure this way.
  2. A constructor has no return value, so it cannot return an error code.
  3. So the statement is true, and the answer is option A, True.

Why the other options are wrong

  • B. 'False' is wrong: exceptions can leave a constructor, while a constructor cannot return an error code.

Tip: Constructors have no return value, so they report errors by throwing an exception.

Question 2Medium

Choose the correct output for given set of code:

class Program
 {
static void Main(string[] args)
      {
try
          {
Console.WriteLine("csharp" + " " + 1/0);
          }
finally
          {
Console.WriteLine("Java");
          }
Console.ReadLine();
      }
  }
  1. csharp 0.
  2. Run time Exception generation.
  3. Compile time error.
  4. Java

Answer: C. Compile time error.

Explanation

The C# compiler evaluates the constant expression 1/0 at compile time and reports error CS0020 (division by constant zero), so the program never runs.

Step-by-step solution

  1. 1/0 is a constant expression of two int literals.
  2. The compiler folds it and rejects division by constant zero (CS0020).
  3. So the code fails to compile: Compile time error.

Why the other options are wrong

  • A. Nothing is printed because the program does not build.
  • B. A runtime exception would need a non-constant divisor.
  • D. Java is only printed by the finally block, which never runs.

Tip: Dividing by a literal zero is a compile error in C#; only a variable zero divisor throws at run time.

Question 3Medium

Can we have multiple catch blocks with try block?

Try
{
    //Code that can generate exception
}
Catch(System.DivideByZeroException ex)
{
    //code to handle the divide by zero exception
}
Catch(system.ArithmaticException ex)
{
    // Code to handle the arithmatic exception
}
  1. Yes
  2. No

Answer: A. Yes

Explanation

Yes we can have as any number of catch blocks with a try block
Though each catch block should catch a different exception.
The order of catch block is important ,because the catch block are examined in order. User should always catch the more specific exception before than the less specific one.The compiler will produce an error if order your catch block so that a later block will never be reached.
Try
{
//Code that can generate exception
}
Catch(System.Exception ex)
{
//code to handle the divide by zero exception
}
Catch(system.ArithmaticException ex)
{
// Code to handle the arithmatic exception
}
The above code will produce a compile time error"A previous catch clause already catches all exception of the a super type ("system.Exception")"

Step-by-step solution

  1. C# allows any number of catch blocks after one try block.
  2. Each catch block should catch a different exception type.
  3. A more specific exception (DivideByZeroException) must come before a general one (ArithmeticException), or the compiler reports an error.
  4. The specific catch comes first here, so the answer is option A, Yes.

Why the other options are wrong

  • B. Several catch blocks are allowed, so 'No' is wrong.

Tip: In catch blocks, put the specific exception first and the general exception after it.

Question 4Medium

Which of the following statement is correct about an Exception?

  1. Its occurs during compilation.
  2. Its occurs during Linking.
  3. Its occurs at run time.
  4. It occurs during Just-in Time compilation.

Answer: C. Its occurs at run time.

Explanation

The C# language's exception handling features help you deal with anyunexpected or exceptional situations that occur when a program is running.

Step-by-step solution

  1. An exception is an error that happens while the program is running.
  2. Compile-time and link-time problems are found before the program runs, so they are not exceptions.
  3. So the answer is option C, at run time.

Why the other options are wrong

  • A. Exceptions are not raised during compilation.
  • B. Linking happens before the program runs, so it is not when exceptions occur.
  • D. JIT compiling is a compile step; the exception happens when the program runs.

Tip: Compile errors come from the compiler; exceptions come from the running program.

Question 5Medium

Exceptions is the Object-Oriented style of handling run-time errors.

  1. True.
  2. False.

Answer: A. True.

Explanation

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program.
They can be because of user, logic or system errors. If a user do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.

Step-by-step solution

  1. Exception handling is the built-in .NET mechanism for detecting and handling run-time errors.
  2. Handling errors through exception objects is the object-oriented way to do it.
  3. So the statement is True, and the correct answer True is correct.

Why the other options are wrong

  • B. False is wrong; exceptions are objects that handle run-time errors in an object-oriented way.

Tip: Exceptions are objects that are caught and handled, which is the object-oriented way to deal with run-time errors.

Question 6Medium

In C#.NET if we do not catch the exception thrown at runtime then which of the following will catch it?

  1. Compiler
  2. CLR
  3. Linker
  4. Loader

Answer: B. CLR

Explanation

Exception occurs at runtime because clear catches.

Step-by-step solution

  1. An exception that no code catches is passed up the call stack.
  2. At the top, the common language runtime (CLR) handles it and ends the program with an error.
  3. The compiler, linker and loader work before or around program start, not during exception handling at run time.
  4. So the answer is option B, CLR.

Why the other options are wrong

  • A. The compiler works before the program runs; it does not catch run-time exceptions.
  • C. The linker combines compiled code; it does not catch exceptions.
  • D. The loader loads the program into memory; it does not catch exceptions.

Tip: Uncaught .NET exceptions are handled by the CLR, which stops the program.

Question 7Medium

Does C# have a 'throws' clause?

  1. Yes.
  2. No.

Answer: B. No.

Explanation

No, unlike Java, C# does not require (or even allow) the developer to specify the exceptions that a method can throw.

Step-by-step solution

  1. C# uses 'throw' keyword to raise exceptions.
  2. It does not have a 'throws' clause like Java.
  3. So, the answer is No.

Why the other options are wrong

  • A. C# does not use 'throws' clause for exception declaration.

Question 8Medium

Which of the following is NOT a .NET Exception class?

  1. Exception
  2. StackMemoryException
  3. DivideByZeroException
  4. OutOfMemoryException

Answer: B. StackMemoryException

Explanation

Because the heap memory is controlled by memory management of. Net.

Step-by-step solution

  1. Exception is a base class for all exceptions in .NET.
  2. StackMemoryException is not a valid exception class in .NET.
  3. DivideByZeroException is a specific exception thrown when division by zero occurs.
  4. OutOfMemoryException is thrown when the system runs out of memory.
  5. The correct answer is StackMemoryException as it is not a .NET Exception class.

Why the other options are wrong

  • A. Exception is a valid base exception class in .NET.
  • C. DivideByZeroException is a recognized .NET exception class.
  • D. OutOfMemoryException is a valid .NET exception class.

Tip: Remember that .NET has a set of predefined exception classes; always check official documentation for accuracy.

Question 9Medium

What is printed?

static int F()
{
    try { return 1; }
    finally { Console.Write("F"); }
}
Console.WriteLine(F());
  1. 1F
  2. 1
  3. F1
  4. F

Answer: C. F1

Explanation

The return value 1 is stored, then finally prints F, then the method returns, and WriteLine prints 1, giving F1.

Step-by-step solution

  1. return 1 is evaluated.
  2. finally runs and writes F.
  3. Caller prints 1.

Why the other options are wrong

  • A. The finally block runs before WriteLine prints the returned value.
  • B. finally still executes, so F is printed.
  • D. The returned value 1 is also printed.

Question 10Hard

Inside a catch block, what is the difference between throw; and throw ex;?

  1. throw; resets the stack trace; throw ex; keeps it
  2. throw; keeps the original stack trace; throw ex; resets it
  3. They are identical
  4. throw; swallows the exception

Answer: B. throw; keeps the original stack trace; throw ex; resets it

Explanation

throw; rethrows the same exception with its original trace. throw ex; restarts the trace from that line, hiding the origin.

Step-by-step solution

  1. Exception caught.
  2. throw; rethrows as-is.
  3. throw ex; resets the trace.

Why the other options are wrong

  • A. It is the other way round.
  • C. They differ in stack trace handling.
  • D. throw; rethrows, not swallows.

Tip: Prefer throw;

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics