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
Nathan Kleyn
November 28, 2013
Programming
160
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
400
Cucumber Automation Testing
nathankleyn
3
270
Git: Everything You Need To Know (And A Few More Things)
nathankleyn
3
190
How The VM Works
nathankleyn
0
180
Other Decks in Programming
See All in Programming
AIエージェントの隔離技術の徹底比較
kawayu
0
430
OSもどきOS
arkw
0
140
今さら聞けないCancellationToken
htkym
0
180
Talking to terminals (and how they talk back) (KotlinConf 2026)
jakewharton
PRO
1
120
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
1
340
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
300
サークル参加から学ぶ、小さな事業の回し方
yuzneri
0
230
自動レビューエンジンの実装と運用 ~レビューのない世界へ~
kurukuru1999
2
250
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
1k
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
140
AI時代になぜ書くのか
mutsumix
0
450
20260514 - build with ai 2026 - build LINE Bot with Gemini CLI
line_developers_tw
PRO
0
470
Featured
See All Featured
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
360
How to build a perfect <img>
jonoalderson
1
5.5k
Automating Front-end Workflow
addyosmani
1370
210k
Scaling GitHub
holman
464
140k
Ruling the World: When Life Gets Gamed
codingconduct
0
240
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
Crafting Experiences
bethany
1
160
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.5k
Typedesign – Prime Four
hannesfritz
42
3k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
210
GraphQLの誤解/rethinking-graphql
sonatard
75
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. ;-)