Java Program to Handle Unchecked Exception
Exceptions are the issues arising at the runtime resulting in an abrupt flow of working of the program. Remember exceptions are never thrown at the compile-time rather always at runtime be it of any type. No exception is thrown at compile time. Throwable Is super-class of all exceptions and errors too. Now there is an urgency to deal with them for which a concept is defined in Java language known as ‘Exception Handling Techniques’
There are two types of exceptions defined as follows
- Checked Exceptions
- Unchecked Exceptions
Real-world Illustration: Exceptions
Consider an employee leaving home for office. He is being monitored by the parent to take ID card stuff and all things as they can think of. Though the employee knows out everything but still being monitored. Now employee leaves out the home still somehow was delayed as his vehicle tyre gets punctured because of which result is that he arrived late to office. Now these wanted things that disrupt his daily routine is referred as Exceptions in Java. Parent actions that helped him though he checked those stuffs but if someday somehow missed and the employee gets things correctly at home itself is referred as ‘checked exception’ in Java. The actions over which parental access does not have any control is referred as ‘unchecked exceptions.’ Here, parent or monitoring authority is referred as ‘Compilers’ in programming languages. Exceptions that can be detected by compilers are checked exceptions and those who can not be detected are called unchecked exceptions.
Approach: Now, in order to deal with exceptions, the concept proposed out are exception handling techniques. Straight away diving onto the concept for unchecked exceptions.
These types of Exceptions occur during the runtime of the program. These are the exceptions that are not checked at a compiled time by the compiler. In Java exceptions under Error and Runtime Exception classes are unchecked exceptions, This Exception occurs due to bad programming.
- Errors class Exceptions like StackOverflow, OutOfMemoryError exception, etc are difficult to handle
- Runtime Exceptions like IndexoutOfBoundException, Nullpointer Exception, etc can be handled with the help of try-Catch Block
There are 2 major Unchecked Exceptions which are faced generally by programmers namely They are discussed below with the help of example implementation and also discussed how to handle them out. Both the major approaches are proposed as below:
Case 1: (Array)IndexoutOfBoundException: This Exception occurs due to accessing the index greater than and equal to the size of the array length. The program will automatically be terminated after this exception. In simpler terms, a memory is being tried to accessed which the current data structure is not holding by itself. Here this exception is defined over data structure namely ‘Arrays‘.
Handling ArrayIndexoutOfBoundException: Try-catch Block we can handle this exception try statement allows you to define a block of code to be tested for errors and catch block captures the given exception object and perform required operations. The program will not terminate.
Case 2: NullPointerException: This exception occurs when trying to access the object reference that has a null value.
Handling Technique for NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.