- An
exception
is an abnormal condition that occurs at run-time.
Or we can say that, - An
exception
is a run-time error. - Catching an
exception
in Java and processing it is calledException Handling
. - In Java, we have objects for exceptions that describe the error that has occurred in the code at run-time.
- When an exception (run-time error) occurs, an object (which contains information about the exception)
is created and
thrown
in the method that has created the error. - Either the method can "handle" the exception itself, or it can pass it to some other method.
- At some point, the exception will be
caught
by a method, and will be processed.
NOTE: Exceptions can be automatically generated by the Java Run-Time Environment, or they can be generated manually by the code that we have written.
try
.catch
.throw
.throws
.finally
.
- The
Throwable
class in thejava.lang
package is the superclass of all exception types in Java. (java.lang.Throwable
). - The
java.lang.Throwable
class has two direct known subclasses; namely, theException
class and theError
class, both in thejava.lang
package. (java.lang.Exception
,java.lang.Error
) - The
java.lang.Exception
class describes the "exceptional conditions" that we should handle in our programs. - The
java.lang.Exception
class has a subclass calledRuntimeException
, also in thejava.lang
package; it describes the types of exceptions that can be automatically handled. - The
java.lang.Error
class describes about problems that cannot be handled by our programs, these are usually major system failures likeOutOfMemoryError
orStackOverflowError
.
We see the output as (shown in red on the IntelliJ console)
Exception in thread "main" java.lang.ArithmeticException: / by zero
at exceptions.UncaughtException.main(UncaughtException.java:14)
Let us understand the above line:
- The very first word
Exception
inException in thread ...
is telling us that an exception has occurred. - We can identify the type of exception that has occurred by looking at the name of the exception,
just see the
java.lang.ArithmeticException
part. Yes, you guessed it right! IT IS indeed a class in thejava.lang
package. - After the
:
the compiler is telling us the reason for the occurrence of this exception, here, it is/ by zero
. - If you look at the last line, you will find a link to the actual file, method and line where the exception has occurred.
- java.lang.LinkageError
- java.lang.ThreadDeath
- java.lang.ClassFormatError
- java.lang.NoClassDefFoundError
- java.lang.VirtualMachineError
- java.lang.InternalError
- java.lang.OutOfMemoryError
- ... and many more
- java.lang.RuntimeException
- java.lang.ClassNotFoundException
- java.lang.ArithmeticException
- java.lang.IllegalAccessException
- java.lang.IllegalArgumentException
- java.util.InputMismatchException
- java.lang.ClassCastException
- java.lang.InterruptedException
- java.lang.ArrayStoreException
- ... and many more
Some Built-In Exceptions in Java
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
ClassCastException
EnumConstantNotPresentException
IllegalArgumentException
IllegalCallerException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IndexOutOfBoundsException
LayerInstantiationException
NegativeArraySizeException
NullPointerException
NumberFormatException
SecurityException
StringIndexOutOfBoundsExeption
TypeNotPresentException
UnsupportedOperationException
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InstantiationException
InterruptedException
NoSuchFieldException
NoSuchMethodException
- They are child classes of the
java.lang.Exception
class. - It is required to handle a
checked
exception usingtry/catch
or to declare them usingthrows
.
- They are also known as runtime exceptions.
- All the Checked (Runtime) exceptions are subclasses (child classes) of
java.lang.RuntimeException
class.