Exceptions – Checked and Runtime Exceptions with Examples

Exceptions – Checked and Runtime Exceptions with Examples: (Custom Exceptions here)
What is an Exception?
Exceptions are events that occurs during program execution.

What are the types of Exception?
There are two types of exceptions,

  • Checked Exception
  • Unchecked / Runtime Exceptions

struct

What is the difference between Error and Exceptions?
Error:
Errors are subclass of Throwable. Errors are something which cannot be recovered and it is best practice not to try-catch the errors.
Example: OutofMemory Error

Exceptions:
Exceptions are subclass of Throwable. Exceptions are something which can be recovered.
Example: IO Exception

Difference Between Checked Exceptions and Runtime Exceptions:

Checked Exceptions Unchecked Exceptions
Checked Exceptions can be caught and handled at Compile Time Unchecked Exceptions occurs at Runtime
Checked Exceptions are direct subclass of exceptions Unchecked Exceptions are subclass of Runtime Exceptions
Examples: IO Exception, Arithmetic exceptions etc. Examples: ArrayIndexOutofBound Exception, NullPointer exception etc

Example for Checked and Runtime Exceptions,

ExceptionHandling.java

Output:
op

CustomExceptions – Here

One thought on “Exceptions – Checked and Runtime Exceptions with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *