Slide 1

Slide 1 text

!!!! Exception Handling The Do’s and Do Not’s

Slide 2

Slide 2 text

The Global Exception Handler

Slide 3

Slide 3 text

You can catch unhandled exceptions thrown in Spring REST handlers in a HandlerExceptionResolver.

Slide 4

Slide 4 text

This documentation for this interface is at: http://docs.spring.io/spring/docs/3.0. x/api/org/springframework/web/servlet/Hand lerExceptionResolver.html

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

For example, the HttpRequestMethodNotSupportedExceptionHand ler could handle requests to an endpoint with a non-supported HTTP verb.

Slide 7

Slide 7 text

The GlobalExceptionHandler would simply proxy onwards to this class.

Slide 8

Slide 8 text

These classes would effectively catch the exception and return a pretty message to the client. They have full access to the exception.

Slide 9

Slide 9 text

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: {}

Slide 10

Slide 10 text

They are Exceptional

Slide 11

Slide 11 text

Do not use Exceptions for flow control. They are for exceptional circumstances, it’s all in the name.

Slide 12

Slide 12 text

public ModelAndView successfulLoginCallback(...) throws ... { try { SocialOAuthResult loginResult = login(…); } catch (RegistrationDetailsRequiredException e) { … } catch (AccountRestrictedException e) { … } }

Slide 13

Slide 13 text

Instead, use a class to encapsulate the return state (or something more appropriate).

Slide 14

Slide 14 text

public ModelAndView successfulLoginCallback(…) throws … { UserLoginResult regResult = login(…); if (regResult.isPartiallyRegistered()) { … } else if (regResult.isRestrictedAccount()) { … } }

Slide 15

Slide 15 text

If you have to use an exception, add it to the GlobalExceptionHandler - even if you don’t think it will ever go uncaught.

Slide 16

Slide 16 text

Handling Exceptions

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Just catch the most relevant exceptions that you know might get thrown.

Slide 20

Slide 20 text

If you don’t preempt every possible exception, and everybody follows the rules, you’ll have only missed something truly exceptional anyway!

Slide 21

Slide 21 text

Catch exceptions just to log and then rethrow them if iffy. Try to come up with a cleaner solution.

Slide 22

Slide 22 text

Throwing Exceptions

Slide 23

Slide 23 text

Unchecked vs. checked exceptions.

Slide 24

Slide 24 text

“Use checked exceptions for conditions for which the caller can reasonably be expected to recover.”

Slide 25

Slide 25 text

“Use runtime exceptions to indicate programming errors.”

Slide 26

Slide 26 text

“Favour the use of standard exceptions.”

Slide 27

Slide 27 text

This is the end. Now go and be unexceptional. ;-)