Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Exception Handling
Search
Nathan Kleyn
November 28, 2013
Programming
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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.7k
Distributed ID Generation
nathankleyn
1
2.4k
Semantic Versioning With Git
nathankleyn
2
400
Cucumber Automation Testing
nathankleyn
3
290
Git: Everything You Need To Know (And A Few More Things)
nathankleyn
3
200
How The VM Works
nathankleyn
0
190
Other Decks in Programming
See All in Programming
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
470
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
280
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.9k
SREの越境 / SRE Collaboration
y0hgi
2
270
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
3.6k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
460
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
16
14k
Webの地図
yosuke_furukawa
PRO
6
4.7k
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
330
iOSDC2026登壇資料.pdf
riofujimon
0
180
すこし踏み込む CancellationToken
htkym
2
940
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
380
Featured
See All Featured
Building Applications with DynamoDB
mza
96
7.2k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
240
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Balancing Empowerment & Direction
lara
6
1.3k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Everyday Curiosity
cassininazir
0
320
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Writing Fast Ruby
sferik
630
63k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3k
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. ;-)