Posts

Showing posts with the label Exception Handling

JAX-RS Exception Handling explained with code samples

There are several ways to perform exception handling in a JAX-RS application. In this article, I will be explaining some of the JAX-RS exception handling mechanisms. Using an ExceptionMapper One way to perform exception handling in a JAX-RS application is using a custom exception mapper. The following code demonstrates a custom exception mapper: @Providerpublic class MyExceptionMapper implements ExceptionMapper<MyException>{@Overridepublic Response toResponse(MyException ex) {return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();}}   This is an Exception Mapper for a custom exception called MyException. It has the @Provider annotation. The MyExceptionMapper class implements the ExceptionMapper interface and overrides the toResponse method. In this method, you can place the correct HTTP status code. In this case, the code uses the HTTP status code for Internal Server Error i.e. code 500. So this code will be sent to the client when MyException occurs. Throwing ...

NullPointerException explained

How many times has a NullPointerException in your code caused the code to break? In this blog post, I will be explaining what a NullPointerException is and what you can do to prevent it.   What is a NullPointerException? A NullPointerException is a RuntimeException . It is an unchecked exception and so does not need to be handled by the code. It occurs when you try to manipulate an object that has a null value.   When does it occur ? A NullPointerException can occur in several scenarios. Scenario 1 – Invoking a method on an object that has a null value. The most common scenarios where a NullPointerException  occurs is when you invoke a method on an object that has a null value. Consider the following code snippet:   String str = "Hello World";System.out.println("Length of the String is "+str.length());str = null;System.out.println("Length of the String is "+str.length());   A variable called “ str ” is initialized to ...

Checked Vs Unchecked exceptions

Image
In this blog post, I will be explaining the difference between checked and unchecked exceptions. Checked exceptions Checked exceptions are those scenarios which can be pre-determined but not prevented . So, during the application design, the developer can anticipate that the program might run into such an exception. However, the programmer can do nothing to prevent these exceptions. So the only course of action is to handle the exception . So in case the code is capable of throwing some checked exception, the compiler will cause a compilation error if the exception is not handled.   Unchecked exceptions Unchecked exceptions are caused either due to programming mistakes or due to hardware failure or some other system error. Because the programmer cannot anticipate them, the compiler does not require you to track unchecked exceptions. Consider the following diagram: exception hierarchy   The boxes in blue represent unchecked exceptions , while the ones in pink represe...

Exception handling keywords explained

I know many of you are confused between the difference between throw and throws or would like to know when a finally statement should be used or have some other questions related to Java’s exception handling mechanism. So, in this blog post, I’m going to try and explain each keyword used in Java’s exception handling mechanism. Try The try keyword is used to specify the code that you want to monitor for exceptions. Code should be enclosed within curly brackets i.e. {} after the try keyword. It is known as the try block. Catch Immediately following the try block, you need to specify a catch clause . The catch block is the exception handler code i.e. code that can process the exception.  In the catch block, you need to specify the exception type that you wish to catch and the code that you want to be executed when an error occurs. Finally When an exception is thrown, the normal flow of execution is altered and so some code may be skipped. However, sometimes there is ...