Exceptions are generated when a recognized condition, usually an error condition, occurs during the execution of a method. There are a number of standard error conditions defined in Java, and you may define your own error conditions as well.
When an exception is generated, it is said to be thrown.
Java syntax includes a system for managing exceptions, by tracking the potential for each method to throw specific exceptions. Note that:
There are two ways to handle an exception:
So, if you use a method in your code that is marked as throwing a particular exception, the compiler will not allow that code unless you handle the exception
Once an exception is thrown, it propagates backward up the chain of methods, from callees to callers, until it is caught. Note that:
If an exception is not caught in your code (which would happen if
marked as throwing the exception) then the JVM will catch
the exception, end that thread of execution, and print a stack trace.
There are cases where the compiler does not enforce these rules. Exceptions that fit this category are called unchecked exceptions.