Posts

Showing posts with the label Java exception handling

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 ...

Java Exception handling explained

In this blog post, I will explain how Java’s exception handling works.   What are the types of errors in Java? You many have encountered compilation errors several times in your code. Compilation errors  are errors that are detected when the code is compiled and even before the code is executed. However, some errors are detected and can occur only when the code is run. Such errors are broadly classified into Exceptions. Java provides a built in class called Exception to handle error conditions. There are also various sub classes of this exception class that handle some specific errors. What is an exception? Whenever an error occurs while executing a statement, Java creates an object of the Exception class. The normal flow of the program  then halts. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, type of exception etc. How do Java exceptions work? When the exception occurs in a method, the proces...