Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle exceptions when writing a library (not an application) - Java

I'm currently writing an Java Wrapper for a RESTful web services API.

I'm now trying to clean up some of the exception handling, and not sure what approach to take. This is a tool intended to be used by Java programmers, so I can't really handle it the same way I would with a end-user application.

If I have a method (connection) that contains code that could throw exceptions, how do I get those exceptions to float up to the end-programmer? and is this even the way I should handle it, depending on them to catch the exceptions? etc...

like image 790
jondavidjohn Avatar asked Apr 05 '11 19:04

jondavidjohn


1 Answers

I suggest you catch the exceptions from the underlying API (unless they really make sense to allow through), and throw a new exception which is more appropriate for your level of abstraction.

Use exception chaining if you don't feel like discarding the cause of the exception.

like image 180
aioobe Avatar answered Sep 24 '22 04:09

aioobe