Un-checked Exceptions
Unchecked exceptions in Java, also known as runtime exceptions, do not require you to declare them using the throws
clause or handle them explicitly using a try-catch
block. They usually occur at runtime and are not checked by the compiler. Below are some common unchecked exceptions along with examples:
NullPointerException: Occurs when you try to access a method or field on a null object.
ArrayIndexOutOfBoundsException: Happens when you try to access an array element with an invalid index.
ArithmeticException: Occurs when you perform an arithmetic operation that's not valid, such as dividing by zero.
NumberFormatException: Raised when you try to parse a string to a number, but the string doesn't represent a valid number.
IllegalArgumentException: Occurs when an illegal argument is passed to a method.
ClassCastException: Raised when you try to cast an object to an incompatible class.
It's important to note that while unchecked exceptions do not require explicit handling, it's still a good practice to handle them when you can anticipate them to avoid unexpected program crashes and improve the robustness of your code.
Last updated