Exception & Error Handling MCQs for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

Exception handling is how a program detects a run-time error, jumps to code that deals with it, and cleans up on the way: try/catch/finally in Java, C++ and JavaScript, and try/except/else/finally in Python. It is the eleventh of 16 topics in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies.

Most questions here are output traces: which lines print, in what order, and what finally does to a return value. The rest test Java's checked and unchecked exceptions. Cleanup ties back to memory management, because exceptions are a common cause of leaks.

Try, catch and finally

An exception is an object that signals an abnormal event, such as dividing by zero or opening a missing file. Code that may fail goes in try. When something is thrown (raised, in Python), the rest of the try block is skipped and the first matching handler runs. finally runs on every exit: normal, by exception, or by return, break or continue.

try:
    print("A")
    x = 1 / 0        # raises ZeroDivisionError
    print("B")       # skipped
except ZeroDivisionError:
    print("C")
finally:
    print("D")       # output: A, C, D

Python adds else, which runs only when the try block raised nothing. It keeps the try block small, so the handler cannot catch errors it was not meant for.

Language Raise Handle Always runs
Java throw catch (Type e) finally, try-with-resources
C++ throw catch (const Type& e), catch (...) destructors (RAII); no finally
Python raise except Type as e finally, with
JavaScript throw catch (e) finally

Exam angle: handlers are tested top to bottom and the first match wins. Put subclasses before superclasses. Java rejects the reverse order at compile time; Python silently runs the broader handler.

Finally and return values

finally runs after a return expression has been evaluated but before the method actually returns. Two rules follow, and they behave the same way in Java and JavaScript:

Code in f() Returns
int x = 10; try { return x; } finally { x = 20; } 10, because the value was already saved
try { return 1; } finally { return 2; } 2, because finally's return replaces it

Returning or throwing from finally also discards any exception in flight, so style guides forbid it. Python 3.14 even warns about return inside finally.

Exam angle: a finally that only changes a variable never changes a value that has already been returned.

Checked vs unchecked exceptions (Java)

In Java every throwable class descends from Throwable, which has two branches: Exception and Error.

Kind Classes Compiler rule Example
Checked Exception except RuntimeException Catch it or declare it with throws IOException, SQLException
Unchecked RuntimeException and its subclasses No rule NullPointerException, ArithmeticException
Unchecked Error and its subclasses No rule; usually not caught OutOfMemoryError, StackOverflowError
public void read() throws IOException {
    FileReader r = new FileReader("a.txt"); // checked: must catch or declare
}

throw raises one exception; throws in a signature declares what may escape. An overriding method may declare the same checked exceptions, narrower ones or none, but never broader ones. C++, Python and JavaScript have no checked exceptions at all.

Exam angle: use checked exceptions for problems a caller can recover from, such as a missing file, and unchecked ones for programming bugs, such as IllegalArgumentException for a bad argument.

Common traps

Worked examples

1. Understanding. In Java, is ArithmeticException checked or unchecked? Answer: unchecked. It extends RuntimeException, so the compiler does not require it to be caught or declared.

2. Application. In Python, what does this print: try: d = {}; d["k"] then except KeyError: print("K") then else: print("E") then finally: print("F")? Answer: K, then F. The lookup raises KeyError, so its handler runs and else is skipped; finally always runs.

3. Analysis. A Java method opens two resources with try-with-resources, and the body throws. In what order do the close calls and the catch block run? Answer: the resources close in reverse order of opening, then the catch block runs. Closing happens as the try block exits, before any catch or finally of the same statement.

Practise

The quiz below has 100 questions on this topic, filterable by tier and difficulty.

Practise: 100 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. In Java, which block runs whether or not the try block throws an exception (unless the JVM exits, for example through System.exit)?

    1. catch
    2. throws
    3. else
    4. finally
    Show answer

    Answer: D. finally

    A finally block runs after the try (and any matching catch) on every normal or exceptional exit, which makes it the place to release resources. catch runs only when a matching exception occurs; throws is part of a method signature; Java has no else clause on try (Python does).

  2. Which Python keyword is used to signal an exception explicitly?

    1. throw
    2. throws
    3. raise
    4. except
    Show answer

    Answer: C. raise

    Python uses raise, for example `raise ValueError("bad age")`. throw is the keyword in Java, C++ and JavaScript; throws appears in Java method signatures; except is Python's handler clause, not a way to raise.

  3. In Java, which of these is a checked exception?

    1. NullPointerException
    2. IOException
    3. ArithmeticException
    4. ArrayIndexOutOfBoundsException
    Show answer

    Answer: B. IOException

    IOException extends Exception but not RuntimeException, so it is checked: the compiler requires you to catch it or declare it with throws. The other three extend RuntimeException, so they are unchecked.

  4. In Java, what is the difference between throw and throws?

    1. throw raises an exception object at a point in the code; throws in a method signature declares exceptions the method may pass to its caller
    2. throw declares exceptions in a signature; throws raises an exception object
    3. They are interchangeable spellings of the same keyword
    4. throw is used for checked exceptions and throws for unchecked exceptions
    Show answer

    Answer: A. throw raises an exception object at a point in the code; throws in a method signature declares exceptions the method may pass to its caller

    `throw new IllegalStateException("closed");` is a statement that raises one exception. `void read() throws IOException` is a declaration telling callers what checked exceptions may escape. Either kind of exception can be thrown with throw.

  5. In Python's try/except/else/finally, when does the else block run?

    1. Only when an exception was raised and caught
    2. Always, just before finally
    3. Only when no except clause matched the exception
    4. Only when the try block finished without raising an exception
    Show answer

    Answer: D. Only when the try block finished without raising an exception

    else runs when the try block completes with no exception, before finally. It is the place for code that should run only on success but whose own errors you do not want the except clauses to catch. finally runs in every case.

  6. In C++, which handler catches an exception of any type?

    1. catch (...)
    2. catch (std::exception& e)
    3. catch (void)
    4. catch (Object e)
    Show answer

    Answer: A. catch (...)

    The ellipsis handler catch (...) matches every exception type, including thrown ints or strings. catch (std::exception&) only matches classes derived from std::exception. catch (void) is invalid, and C++ has no universal Object base class.

  7. In Java, which class is the direct superclass of both Exception and Error?

    1. RuntimeException
    2. Object
    3. Throwable
    4. BaseException
    Show answer

    Answer: C. Throwable

    Only Throwable and its subclasses can be thrown and caught in Java. Throwable has two direct subclasses: Exception (conditions a program may handle) and Error (serious JVM problems). RuntimeException is a subclass of Exception; BaseException is Python's root class.

  8. What does it mean for an exception to be checked in Java?

    1. The JVM checks for it before the program starts
    2. It is caught automatically by the JVM and logged
    3. The compiler requires every method that can throw it either to catch it or to declare it with throws
    4. It can only be thrown by code in the java.lang package
    Show answer

    Answer: C. The compiler requires every method that can throw it either to catch it or to declare it with throws

    Checked exceptions (Exception subclasses other than RuntimeException) are subject to the catch-or-declare rule at compile time. Unchecked exceptions (RuntimeException and Error subclasses) are exempt. The check is static, by the compiler, not at run time.

  9. In C++, why should you usually catch exceptions by reference, as in `catch (const std::exception& e)`?

    1. Catching by value is a compile error in C++
    2. References make the exception object be freed earlier
    3. It lets the handler catch exceptions of any type, including int
    4. It avoids copying the exception and prevents slicing, so overridden members such as what() behave polymorphically
    Show answer

    Answer: D. It avoids copying the exception and prevents slicing, so overridden members such as what() behave polymorphically

    Catching a derived exception by value copies only the base-class part (object slicing), so virtual calls such as what() would use the base version. A reference binds to the original object with its full type. Catching by value is legal, just worse; only catch (...) catches every type.

  10. Which interface must a class implement to be used in a Java try-with-resources statement?

    1. AutoCloseable (Closeable extends it)
    2. Serializable
    3. Runnable
    4. Iterable
    Show answer

    Answer: A. AutoCloseable (Closeable extends it)

    try (Res r = new Res()) { ... } calls r.close() automatically when the block exits, so the resource type must implement java.lang.AutoCloseable. java.io.Closeable is a sub-interface used by I/O streams. The other interfaces have nothing to do with cleanup.

FAQs

What is the difference between an error and an exception?

In Java both are Throwables: Exception covers conditions a program may reasonably handle, while Error signals serious problems such as OutOfMemoryError that applications normally should not catch. In everyday speech, "error" also means a compile-time mistake, such as a syntax error, which is not an exception at all.

Does C++ have a finally block?

No. C++ relies on RAII instead: put cleanup in a destructor, and it runs automatically when the object goes out of scope, including during stack unwinding after a throw. That is why std::vector, std::unique_ptr and std::lock_guard are exception-safe without a finally.

When should I raise my own exception class?

Create one when callers need to tell your error apart from built-in ones, such as InsufficientFunds in a banking module. In Python inherit from Exception; in Java extend Exception for a checked exception or RuntimeException for an unchecked one. Give it a clear message.

Is it bad to catch every exception?

Catching everything, such as catch (Exception e) or a bare except:, hides bugs you did not expect and can swallow interrupts. Catch the specific exceptions you can actually handle, and let the rest propagate to a top-level handler that logs them. Our debugging and testing page shows how to trace them.

Next step

Move on to Modules, Packages & Libraries MCQs, see all topics on the Programming MCQs hub, or test yourself in an NSCT mock test.