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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Nathan Kleyn
November 28, 2013
Programming
0
140
Exception Handling
The do's and do not's of exception handling and throwing, specifically with Java and Spring.
Nathan Kleyn
November 28, 2013
Tweet
Share
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
250
Git: Everything You Need To Know (And A Few More Things)
nathankleyn
3
180
How The VM Works
nathankleyn
0
160
Other Decks in Programming
See All in Programming
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
AI時代の認知負荷との向き合い方
optfit
0
160
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
MUSUBIXとは
nahisaho
0
130
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
110
AI巻き込み型コードレビューのススメ
nealle
1
160
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
ぼくの開発環境2026
yuzneri
0
190
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
190
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
690
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
57
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
90
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
110
Information Architects: The Missing Link in Design Systems
soysaucechin
0
770
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
180
Done Done
chrislema
186
16k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
300
BBQ
matthewcrist
89
10k
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. ;-)