Tutorials ADO.NET Core Tutorial
Exception Handling — Complete Guide
Exception Handling — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ADO.NET Core Tutorial on Toolliyo Academy.
On this page
ADO.NET Core Tutorial · Lesson 36 of 100
Exception Handling
Foundations ✓ → SQL & safety → Production → Projects
SQL & safety · 2 — Procs, tx, performance · ~6 min · Module 4: Transactions and Error Handling
What is this?
SqlException carries Number, message, and procedure info — map to domain errors and log safely.
Why should you care?
ShopNest APIs must not leak connection strings or table dumps to clients.
See it live — copy this example
Use a .NET console or API project with SQL Server LocalDB. Run dotnet run after pasting.
try { await cmd.ExecuteNonQueryAsync(ct); }
catch (SqlException ex)
{
Console.WriteLine(ex.Number);
if (ex.Number == 547) throw new InvalidOperationException("FK violation", ex);
throw;
}
What happened?
- Catch near the repo.
- Log Number + sanitized message.
- Translate 2627/547 to 409/400-style domain errors.
Practice next
- Catch SqlException.
- Switch on Number.
- Log without secrets.
- Handle 2627 unique key.
- Include procedure name in logs.
Remember
Use SqlException.Number. Map to domain. Sanitize logs.
ShopNest SQL error mapping
FK failures become 400s.
Outcome: Clients see clear errors; logs stay useful.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!