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
0
130
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.5k
Distributed ID Generation
nathankleyn
1
2.3k
Semantic Versioning With Git
nathankleyn
2
360
Cucumber Automation Testing
nathankleyn
3
220
Git: Everything You Need To Know (And A Few More Things)
nathankleyn
3
160
How The VM Works
nathankleyn
0
140
Other Decks in Programming
See All in Programming
もうちょっといいRubyプロファイラを作りたい (2025)
osyoyu
0
390
プロパティベーステストによるUIテスト: LLMによるプロパティ定義生成でエッジケースを捉える
tetta_pdnt
0
280
rage against annotate_predecessor
junk0612
0
160
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
7
3.3k
Rancher と Terraform
fufuhu
2
240
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
510
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
100
Kiroで始めるAI-DLC
kaonash
2
580
時間軸から考えるTerraformを使う理由と留意点
fufuhu
14
4.6k
開発チーム・開発組織の設計改善スキルの向上
masuda220
PRO
19
11k
ソフトウェアテスト徹底指南書の紹介
goyoki
1
150
アセットのコンパイルについて
ojun9
0
120
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
53
8.9k
The Power of CSS Pseudo Elements
geoffreycrofte
77
6k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Visualization
eitanlees
148
16k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
51
5.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Scaling GitHub
holman
463
140k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
520
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
The Art of Programming - Codeland 2020
erikaheidi
55
13k
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. ;-)