Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java exception class to use for HTTP errors?

I am using Apache HttpClient and would like to communicate HTTP errors (400 Bad Request, 404 Not Found, 500 Server Error, etc.) via the Java exception mechanism to the calling code. Is there an exception in the Java standard library or in a widely used library that would be appropriate to use or to subclass for this purpose?

The alternative is to check status return codes. This appears to be the HttpClient design philosophy, but since these errors are truly exceptional in my app, I would like to have the stack trace and other nice exception things set up for me when they happen.

like image 957
Steven Huwig Avatar asked Oct 17 '08 13:10

Steven Huwig


People also ask

What is an HTTP exception?

The HTTPException exception represents a XML/HTTP fault. Since there is no standard format for faults or exceptions in XML/HTTP messaging, only the HTTP status code is captured.

What Java exception should I throw?

Only checked exceptions are required to be thrown using the throws keyword. Unchecked exceptions don't need to be thrown or handled explicitly in code.

Which class is used for handling errors?

All exception and error types are subclasses of class Throwable, which is the base class of the hierarchy.

Which is the best class of all the exceptions and errors in Java?

The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class. Throwable has two direct subclasses - Exception and Error.


1 Answers

If it's not an Exception in HttpClient design philosophy, but an Exception in your code, then create your own Exception classes. ( As a subclass of org.apache.commons.httpclient.HttpException )

like image 124
asalamon74 Avatar answered Oct 08 '22 18:10

asalamon74