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.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What is the difference between throw; and throw ex; in a catch block?
Answered in the lesson Exceptions: try, catch, finally and filters
-
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
-
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.
-
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
-
- Where should catch (Exception) live in this job?
- How does the job's exit status become trustworthy?
- What must a log entry for a failed file contain?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- When is wrapping the exception the better fix?
- How would an exception filter help with the logging?
- How do you rethrow later, after the catch block has ended?
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
-
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
-
- Does a try block cost anything when nothing is thrown?
- Which exceptions should the import still catch?
- How do you show that the change kept the same results?
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
-
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
-
- What does the change mean for existing callers of the API?
- Which failures of the same service should stay exceptions?
- How do you stop callers from ignoring the new result?
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
-
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
-
- How does the mobile app retry a timeout without paying twice?
- What goes into the response body of a refusal, and what never does?
- How do internal callers tell a refusal from an outage?
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
-
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
-
- Why did C# leave checked exceptions out?
- What belongs in an exception tag of the XML documentation?
- How would you catch an undocumented exception before release?
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
-
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
-
- Why are some files empty and others cut off in the middle?
- Would calling Flush after every WriteLine be a good fix?
- How would you catch the same mistake in code that is not written yet?
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
-
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
-
- What does a tick that is already running do while Dispose runs?
- What changes if the class is not sealed?
- Should the class also implement IAsyncDisposable?
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
-
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
-
- How does Java's try-with-resources behave in the same case?
- What should a Dispose do with a failure of its own?
- How would you test that the parsing error now reaches the log?
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