Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Exception Handling
Search
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
380
Cucumber Automation Testing
nathankleyn
3
230
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
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
470
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
6
2.2k
안드로이드 9년차 개발자, 프론트엔드 주니어로 커리어 리셋하기
maryang
1
110
チームをチームにするEM
hitode909
0
330
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
3.6k
Full-Cycle Reactivity in Angular: SignalStore mit Signal Forms und Resources
manfredsteyer
PRO
0
140
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
700
FluorTracer / RayTracingCamp11
kugimasa
0
230
tparseでgo testの出力を見やすくする
utgwkk
2
220
S3 VectorsとStrands Agentsを利用したAgentic RAGシステムの構築
tosuri13
6
310
20 years of Symfony, what's next?
fabpot
2
360
マスタデータ問題、マイクロサービスでどう解くか
kts
0
100
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
46
7.8k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
Git: the NoSQL Database
bkeepers
PRO
432
66k
What's in a price? How to price your products and services
michaelherold
246
13k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.3k
4 Signs Your Business is Dying
shpigford
186
22k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
710
Music & Morning Musume
bryan
46
7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
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. ;-)