Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Exception Handling
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Nathan Kleyn
November 28, 2013
Programming
150
0
Share
Exception Handling
The do's and do not's of exception handling and throwing, specifically with Java and Spring.
Nathan Kleyn
November 28, 2013
More Decks by Nathan Kleyn
See All by Nathan Kleyn
On our CI and Builds
nathankleyn
0
2.6k
Distributed ID Generation
nathankleyn
1
2.3k
Semantic Versioning With Git
nathankleyn
2
390
Cucumber Automation Testing
nathankleyn
3
260
Git: Everything You Need To Know (And A Few More Things)
nathankleyn
3
190
How The VM Works
nathankleyn
0
170
Other Decks in Programming
See All in Programming
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
560
UIの境界線をデザインする | React Tokyo #15 メイントーク
sasagar
2
370
おれのAgentic Coding 2026/03
tsukasagr
1
150
AWSコミュニティ活動は顧客のクラウド推進に効くのか / Do AWS community activities help customers adopt the cloud?
seike460
PRO
0
140
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
180
Back to the roots of date
jinroq
0
260
t *testing.T は どこからやってくるの?
otakakot
1
690
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
570
Claude Codeをカスタムして自分だけのClaude Codeを作ろう
terisuke
0
140
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
3k
AI-DLC Deep Dive
yuukiyo
9
4.4k
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
The Invisible Side of Design
smashingmag
303
52k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
270
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
210
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
220
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
270
Design in an AI World
tapps
1
200
Heart Work Chapter 1 - Part 1
lfama
PRO
6
35k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
140
Designing for Timeless Needs
cassininazir
0
200
Exploring anti-patterns in Rails
aemeredith
3
320
The World Runs on Bad Software
bkeepers
PRO
72
12k
Transcript
!!!! Exception Handling The Do’s and Do Not’s
The Global Exception Handler
You can catch unhandled exceptions thrown in Spring REST handlers
in a HandlerExceptionResolver.
This documentation for this interface is at: http://docs.spring.io/spring/docs/3.0. x/api/org/springframework/web/servlet/Hand lerExceptionResolver.html
When an exception is caught, you could add code to
check for another class that could this exception by returning a more specific message, defaulting to a standard message if one could not be found.
For example, the HttpRequestMethodNotSupportedExceptionHand ler could handle requests to an
endpoint with a non-supported HTTP verb.
The GlobalExceptionHandler would simply proxy onwards to this class.
These classes would effectively catch the exception and return a
pretty message to the client. They have full access to the exception.
A log should be made when an exception wasn’t handled
by a specific class: I wasn't able to find a custom ExceptionHandler for the exception '{}' are you sure you want to handle this with the GlobalExceptionHandler? exception: {}
They are Exceptional
Do not use Exceptions for flow control. They are for
exceptional circumstances, it’s all in the name.
public ModelAndView successfulLoginCallback(...) throws ... { try { SocialOAuthResult loginResult
= login(…); } catch (RegistrationDetailsRequiredException e) { … } catch (AccountRestrictedException e) { … } }
Instead, use a class to encapsulate the return state (or
something more appropriate).
public ModelAndView successfulLoginCallback(…) throws … { UserLoginResult regResult = login(…);
if (regResult.isPartiallyRegistered()) { … } else if (regResult.isRestrictedAccount()) { … } }
If you have to use an exception, add it to
the GlobalExceptionHandler - even if you don’t think it will ever go uncaught.
Handling Exceptions
Do not catch Throwable. If you find yourself needing to
do this, please ask the team to weigh in - chances are, you’re doing it wrong.
None
Just catch the most relevant exceptions that you know might
get thrown.
If you don’t preempt every possible exception, and everybody follows
the rules, you’ll have only missed something truly exceptional anyway!
Catch exceptions just to log and then rethrow them if
iffy. Try to come up with a cleaner solution.
Throwing Exceptions
Unchecked vs. checked exceptions.
“Use checked exceptions for conditions for which the caller can
reasonably be expected to recover.”
“Use runtime exceptions to indicate programming errors.”
“Favour the use of standard exceptions.”
This is the end. Now go and be unexceptional. ;-)