Throw statement
Sometimes we can create exception objects explicitly and we can hand over to the JVM manually by using the throw keyword.
Example:
• The result of the following 2 programs is exactly the same.
Note: In general we can use throw keyword for customized exceptions but not for predefined exceptions.
Case 1: throw e;
• If e refers null then we will get NullPointerException.
Example:
Case 2: After throw statement, we can’t take any statement directly otherwise we will get a compile-time error saying an unreachable statement.
Example:
Case 3: We can use throw keyword only for Throwable types otherwise we will get a compile-time error saying incomputable types.
Example:
Comments
Post a Comment