These are problems that occur at runtime and compile time.
it mainly occurs in the code written by the developers.
Checked
Checked at compile time.
All exceptions which inherit from Exception class (including itself) except RuntimeException are checked exception.
For checked exception you need to specify throws beside method signature if you don’t handle it.
If you call a method which has a checked exception specified or throw a checked exception then you must handle it or you need to specify throws keyword beside method signature if you don’t handle it.
Exception
IOException
SQLException
DataFormatException
public class Cat { public void dance() throws SQLException { throw new SQLException(); }}public class Zoo { /** * If you don't handle checked exceptions then you have to specify throws clause */ public static void danceCat(Cat cat) { try { cat.dance(); } catch (SQLException e) { throw new RuntimeException(e); } }}
Unchecked
Occur at runtime
All exceptions that inherit from RuntimeException (including itself) are unchecked.
For unchecked exception it is not required to specify throws keyword.
Exception:
RuntimeException
NullPointerException — calling an action on null object
ArithmeticException — invalid math operation like diving by zero
DateTimeException — problem while calculating a date-time
Custom Exceptions
For Checked Exceptions you can extend Exception class
For Unchecked Exceptions you can extend RuntimeException class (which internally extends Exception)
public class FibonacciInputException extends Exception { public FibonacciInputException(String message) { super(message); }}
Error
It indicates serious problems that a reasonable application should not try to catch.
Most such errors are abnormal conditions like lack of system resources.
Error
IOError — Serious issue with underlying filesystem
AssertionError
VirtualMachineError — indicates JVM running out of resources
InternalError
UnknownError
OutOfMemoryError
StackOverflowError
It is possible to throw error but it is not recommended
try { throw new Error();} catch (Throwable ex) { // you need to catch via "Throwable" superclass System.out.println("found the error");}
try-catch-finally
The finally block executes regardless of whether an exception is thrown or caught.
We generally use the finally block to execute clean up code like closing connections, closing files, or freeing up threads, as it executes regardless of an exception.
There can only be one finally but there can be multiple catch blocks.
No matter whether exception is handled or not, or return statement is present or not, finally will always execute.
return statement in finally block will always override try or catch’s return statements if present.