Exception handling

As you already know, an exception interrupts the normal execution of a program. Normally this is not what we want to happen. Luckily, it is possible to write some code that will handle the exception without stopping the whole program. To do that, Java provides an exception handling mechanism that works with both checked and unchecked exceptions.

Hierarchy of exceptions

Java is primarily an object-oriented language. In such a paradigm, all exceptions are considered objects of special classes organized into a class hierarchy. Understanding this hierarchy is essential both for job interviews and daily programming practice. Hierarchy of exceptions The following picture illustrates the simplified hierarchy of exceptions: The base class for all exceptions is java.lang.Throwable. This class provides a set of common methods for all…

NPE

What is NPE Java provides a special type of value called null to indicate that no actual value is assigned to a reference variable. This value may cause one of the most frequent exceptions called NullPointerException (often referred to as NPE for short). It occurs when a program attempts to use a variable with the null value. To avoid an NPE, the programmer must ensure that the objects are initialized before their use.

What is an exception

Some errors in your code do not prevent the program from running. In this case, the program will only crash while trying to execute a “broken” line: a line with an error called an exception. Thus, exceptions are the errors detected during the program execution (at runtime), whereas syntax errors are those detected during compiling the program into byte-code. An exception interrupts the normal execution of a program.