Java Exception Types
In Java, there are two main types of exceptions: checked exceptions and unchecked exceptions.
Checked Exceptions:
Checked exceptions are those exceptions that are checked at compile time. This means that the compiler will check if the method that might throw the exception is properly handled. If it is not handled, the compiler will give a compile-time error. Examples of checked exceptions includeIOException
,SQLException
, andClassNotFoundException
.Unchecked Exceptions:
Unchecked exceptions are those exceptions that are not checked at compile time. This means that the compiler does not check if the method that might throw the exception is properly handled. Unchecked exceptions are usually caused by problems in the program's logic or environment. Examples of unchecked exceptions includeNullPointerException
,ArrayIndexOutOfBoundsException
, andArithmeticException
.
In addition to these two main types of exceptions, there are also some other types of exceptions in Java, such as:
Error:
Errors are serious problems that usually cannot be handled by the program itself. Examples of errors includeOutOfMemoryError
andStackOverflowError
.RuntimeException:
RuntimeExceptions are unchecked exceptions that can occur at runtime. Examples of RuntimeExceptions includeNullPointerException
andIllegalArgumentException
.ExceptionInInitializerError:
This exception occurs when an error occurs while initializing a static initializer. The error is wrapped in this exception and thrown.StackOverflowError:
This exception occurs when the stack is full and no more method calls can be made.NoSuchMethodError:
This exception occurs when a method is called that does not exist.ClassNotFoundException:
This exception occurs when the class loader cannot find a class that is required by the program.IllegalAccessException:
This exception occurs when a method or field cannot be accessed due to the access modifier.IllegalArgumentException:
This exception occurs when a method is called with an invalid argument.InterruptedException:
This exception occurs when a thread is interrupted while it is sleeping or waiting.