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
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.6k
Distributed ID Generation
nathankleyn
1
2.4k
Semantic Versioning With Git
nathankleyn
2
400
Cucumber Automation Testing
nathankleyn
3
280
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
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
120
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
340
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
420
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
190
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
130
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
710
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
460
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
120
その節約、円になってますか?
isamumumu
0
560
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
210
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
2
1.3k
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
160
Featured
See All Featured
Optimizing for Happiness
mojombo
378
71k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
GraphQLとの向き合い方2022年版
quramy
50
15k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
280
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
520
Agile that works and the tools we love
rasmusluckow
331
22k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
230
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
200
Accessibility Awareness
sabderemane
1
170
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
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. ;-)