Refactoring 035 - Separate Exception Types
Distinguish your technical failures from business rules

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Search for a command to run...
Distinguish your technical failures from business rules

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
No comments yet. Be the first to comment.
Approve the logic once, then let it run the same way forever.

Different stages need different brains.

One Second Brain doesn't scale past one skull.

Style errors double when nobody enforces them.

Know who speaks before the skill runs TL;DR: Always define a clear role at the top of every skill file so you know whose perspective drives the execution. Common Mistake ❌ You write a skill full of

TL;DR: Use separate exception hierarchies for business and technical errors.
public void Withdraw(int amount) {
if (amount > Balance) {
throw new Exception("Insufficient funds");
// You might want to show this error to end users
}
if (connection == null) {
throw new Exception("Database not available");
// Internal error, log and notify operators.
// Fail with a more generic error
}
Balance -= amount;
}
// 1. Identify business exceptions
public class BusinessException : Exception {}
public class InsufficientFunds : BusinessException {}
// 2. Identify technical exceptions
public class TechnicalException : Exception {}
public class DatabaseUnavailable : TechnicalException {}
public void Withdraw(int amount) {
// 3. Use the correct hierarchy
if (amount > Balance) {
throw new InsufficientFunds();
}
if (connection == null) {
throw new DatabaseUnavailable();
}
// 4. Apply safe logic
Balance -= amount;
}
// 5. Adjust handlers in the calling code
[X] Manual
This refactoring is safe if you apply it gradually and update your code with care.
You must ensure all thrown exceptions are caught at the proper architectural level.
You make the code clearer and more predictable.
You express technical failures and business rules separately, taking corrective actions with different stakeholders.
You also reduce confusion for the caller and improve maintainability.
This refactoring strengthens the mapping between real-world concepts and code representation.
In reality, business rule violations and technical failures are fundamentally different situations.
Business exceptions represent expected alternative flows in your domain model.
Technical exceptions represent unexpected system problems that break the execution environment.
By separating these concerns, your code more accurately reflects the real-world distinction between "business says no" and "system cannot proceed".
You need discipline to maintain two hierarchies.
If you misuse them, the benefits are lost. You also need to communicate the contract clearly to the clients of your code.
You should also create your own integrity tests to enforce these rules.
Suggested Prompt: 1. Identify business exceptions 2. Identify technical exceptions 3. Create two separate hierarchies 4. Update code to throw the right one
- Adjust handlers accordingly
| Without Proper Instructions | With Specific Instructions |
| ChatGPT | ChatGPT |
| Claude | Claude |
| Perplexity | Perplexity |
| Copilot | Copilot |
| You | You |
| Gemini | Gemini |
| DeepSeek | DeepSeek |
| Meta AI | Meta AI |
| Grok | Grok |
| Qwen | Qwen |
[X] Intermediate
This article is part of the Refactoring Series.