- An
exceptionis an abnormal condition that occurs at run-time.
Or we can say that, - An
exceptionis a run-time error. - Catching an
exceptionin 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
thrownin 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
caughtby 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
Throwableclass in thejava.langpackage is the superclass of all exception types in Java. (java.lang.Throwable). - The
java.lang.Throwableclass has two direct known subclasses; namely, theExceptionclass and theErrorclass, both in thejava.langpackage. (java.lang.Exception,java.lang.Error) - The
java.lang.Exceptionclass describes the "exceptional conditions" that we should handle in our programs. - The
java.lang.Exceptionclass has a subclass calledRuntimeException, also in thejava.langpackage; it describes the types of exceptions that can be automatically handled. - The
java.lang.Errorclass describes about problems that cannot be handled by our programs, these are usually major system failures likeOutOfMemoryErrororStackOverflowError.
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
ExceptioninException 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.ArithmeticExceptionpart. Yes, you guessed it right! IT IS indeed a class in thejava.langpackage. - 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
ArithmeticExceptionArrayIndexOutOfBoundsExceptionArrayStoreExceptionClassCastExceptionEnumConstantNotPresentExceptionIllegalArgumentExceptionIllegalCallerExceptionIllegalMonitorStateExceptionIllegalStateExceptionIllegalThreadStateExceptionIndexOutOfBoundsExceptionLayerInstantiationExceptionNegativeArraySizeExceptionNullPointerExceptionNumberFormatExceptionSecurityExceptionStringIndexOutOfBoundsExeptionTypeNotPresentExceptionUnsupportedOperationExceptionClassNotFoundExceptionCloneNotSupportedExceptionIllegalAccessExceptionInstantiationExceptionInterruptedExceptionNoSuchFieldExceptionNoSuchMethodException
- They are child classes of the
java.lang.Exceptionclass. - It is required to handle a
checkedexception usingtry/catchor to declare them usingthrows.
- They are also known as runtime exceptions.
- All the Checked (Runtime) exceptions are subclasses (child classes) of
java.lang.RuntimeExceptionclass.

