Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

C# & .NET (C# 14, .NET 10 LTS) Module 13

Exceptions and resource cleanup interview questions

Interview questions on exceptions and resource cleanup: warm-ups, then what each harder question tests and how a good answer starts.

12 questions: 3 warm-ups and 9 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is the difference between throw; and throw ex; in a catch block?

    Answered in the lesson Exceptions: try, catch, finally and filters

  2. Fresher

    When should a method throw an exception, and when should it return a failure?

    Answered in the lesson Designing exceptions, guard clauses and results

  3. Fresher

    What does a using statement do, and what does the compiler turn it into?

    Answered in the lesson using, IDisposable and IAsyncDisposable

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A nightly job imports bank statements. Each file is processed inside try { … } catch (Exception e) { log.Warning(e.Message); }, and the job always ends with exit status 0. Customers report that the payments of some days never appeared. What is wrong with this error handling, and how would you restructure it?

    What it tests
    Catching at the level that can act, letting unknown failures reach a boundary that reports them, honest exit statuses, and logging enough to diagnose a failure.
    First step
    Sort the failures the import can meet: a line with a bad amount, a share that is busy for a moment, and everything else, such as a changed file layout or a full disk. Then ask what the catch block does with each kind: it logs one line, drops the stack trace, and reports success either way.
    Follow-ups

    From the lesson Exceptions: try, catch, finally and filters

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to mid

    Production logs show a NullReferenceException whose stack trace starts inside the catch block of OrderService.Save, not at the line that read the null. What most likely happened, and how do you make the next incident point at the real line?

    What it tests
    Reading a stack trace, throw; against throw ex;, wrapping with an inner exception, and rethrowing later with ExceptionDispatchInfo.
    First step
    Read the catch blocks of OrderService.Save. A trace that starts there means the exception was thrown again from that line: by throw ex;, or as a new exception built without the original as its inner exception. Compare what each form keeps of the original frames.
    Follow-ups

    From the lesson Exceptions: try, catch, finally and filters

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A CSV import handles 2 million rows a night, and about 5 % of the amount fields fail to parse. It calls decimal.Parse inside try and catch for every field. A teammate proposes removing every try block in the project to make it faster. What do you advise, and how do you measure it?

    What it tests
    The cost of throwing against checking, Try methods for expected failures, what a try block costs on its own, and measuring a change before and after.
    First step
    Count the exceptions before guessing: 5 % of 2 million is 100,000 failed parses a night. Measure one throw and catch against a failed TryParse on the server, then separate the failures the import expects from the ones it does not.
    Follow-ups

    From the lesson Exceptions: try, catch, finally and filters

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to senior

    A wallet service throws InsufficientFundsException whenever a balance is too low. At peak it throws 40,000 of them an hour, and the team believes exceptions are slowing it down. Would you change the design? How do you decide, and how do you measure the effect?

    What it tests
    Telling expected outcomes from errors, the real cost of throwing, result types and Try methods, and measuring before changing a public API.
    First step
    Separate the two claims. Is a low balance an error or an outcome that careful users meet every day? Then put a number on the cost: measure one throw and catch of this exception on the server, multiply by 40,000 an hour, and compare it with the request time the service actually spends.
    Follow-ups

    From the lesson Designing exceptions, guard clauses and results

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Senior to staff

    Design the error model of a money-transfer API that a mobile app and other internal services call over HTTP. Which failures are exceptions inside the service, which are results, and what does each kind of caller receive?

    What it tests
    Classifying failures, result types with reasons, mapping outcomes to HTTP responses, retries and idempotency, and what is safe to tell a caller.
    First step
    List the failures before choosing types: bad requests, a low balance, a frozen account, a concurrent change, a ledger that times out and a bug. Mark each as a caller's mistake, an expected outcome or an environment failure, and decide for each what the app's user must see.
    Follow-ups

    From the lesson Designing exceptions, guard clauses and results

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Mid to senior

    A team that moved from Java wants every public method of a C# library to declare what it throws, with a build step that fails whenever a method can throw something new. What do you recommend instead, and why?

    What it tests
    Why C# has no checked exceptions, documenting exceptions, what the compiler and analyzers can check, and designing APIs whose failures callers can handle.
    First step
    Ask what the team wants to protect: callers being surprised by an exception they never handled. Then look at what C# offers for that: documentation the compiler checks, built-in exception types with clear meanings, result types for expected outcomes, and analyzers that a build can enforce.
    Follow-ups

    From the lesson Designing exceptions, guard clauses and results

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  7. Fresher to mid

    A nightly job writes 2,000 CSV reports. A few come out empty and some stop halfway through, and no exception is logged. Each report is written with new StreamWriter(path) and a WriteLine for every row. What is going wrong, and how do you fix it and show that the fix works?

    What it tests
    Buffered writers, what Dispose does, using statements and declarations, and a test that fails before the fix and passes after it.
    First step
    Look for where each writer is disposed. If nothing calls Dispose or Flush, the last part of every report waits in the writer's buffer, and whether it ever reaches the disk depends on what happens to the process. Reproduce it with one report, measuring the file before the writer is disposed and after.
    Follow-ups

    From the lesson using, IDisposable and IAsyncDisposable

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Mid to senior

    A class opens a FileStream in its constructor and starts a System.Threading.Timer that writes a line to the stream every second. How do you make the class disposable, in what order does it release the two, and does it need a finalizer?

    What it tests
    Ownership of disposable fields, the order of cleanup, idempotent Dispose, ObjectDisposedException, sealed against unsealed types, and when a finalizer is justified.
    First step
    List what the class owns: the stream it opened and the timer it started, both disposable. Then follow a tick that fires while Dispose runs: if the stream closes first, that tick writes to a closed stream. Decide the order that makes the last tick harmless.
    Follow-ups

    From the lesson using, IDisposable and IAsyncDisposable

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  9. Mid to senior

    An upload service's logs show only "The share dropped the connection" errors, thrown while uploads are disposed, and never the parsing errors that operators know are happening. What is going on, and how do you change the disposable types and the code that uses them?

    What it tests
    Exceptions thrown from finally blocks and Dispose, how using is compiled, Dispose methods that do not throw, and logging both failures without losing either.
    First step
    Recall that using is a try statement with Dispose in its finally block. Ask what happens to an exception that is leaving the block when Dispose throws a second one, and check the upload type's Dispose for anything that can throw.
    Follow-ups

    From the lesson using, IDisposable and IAsyncDisposable

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All C# & .NET (C# 14, .NET 10 LTS) interview questions · The C# & .NET (C# 14, .NET 10 LTS) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.