The difference between checked and unchecked exception is that unchecked exception will not cause compile error even though it is not caught or not declared to be thrown.

Configuration
Java Compilation:
Java Runtime:
JDK 11.0.12
JRE HotSpot 11.0.12
Unchecked exceptions are all the classes inheriting from RuntimeException, including RuntimeException itself.
Checked exceptions are all the classes inheriting from Exception excluding RuntimeException.
The difference between checked and unchecked exception is that unchecked exception will not cause compile error even though it is not caught or not declared to be thrown.
For checked exceptions, if it is not caught or declared to be thrown, there will be compilation error. See the below checked exception:
public class MyCheckedException extends Exception { public MyCheckedException(String message) { super(message); } }
Using it in main:
public class MainClass { public static void main(String[] args) { for (int count = 0; count < 10; count++) { if (count == 5) { throw new MyCheckedException("bumped into an issue!"); } } } }
There will be compilation error:
java: unreported exception MyCheckedException; must be caught or declared to be thrown
To fix the error, we either catch the exception or declare the exception to be thrown
We can catch the exception:
public class MainClass { public static void main(String[] args) { try { for (int count = 0; count < 10; count++) { if (count == 5) { throw new MyCheckedException("bumped into an issue!"); } } } catch (MyCheckedException mce) { System.out.println("got MyCheckedException: " + mce.getMessage()); } } }
Or we can declare the exception to be thrown:
public class MainClass { public static void main(String[] args) throws MyCheckedException { for (int count = 0; count < 10; count++) { if (count == 5) { throw new MyCheckedException("bumped into an issue!"); } } } }
If MyCheckedException is changed to extend RuntimeException (also is renamed):
public class MyUncheckedException extends RuntimeException { public MyUncheckedException(String message) { super(message); } }
Then there will no compilation error even though the exception is not caught or declared to be thrown:
public class MainClass { public static void main(String[] args) { for (int count = 0; count < 10; count++) { if (count == 5) { throw new MyUncheckedException("bumped into an issue!"); } } } }
Since it has no compilation error, it is very easy to miss the handling. At the end there will be error on runtime:
Exception in thread "main" MyUncheckedException: bumped into an issue! at MainClass.main(MainClass.java:9)