Java throw keyword

At runtime if our java program violates JVM rules then JVM itself will creates and throws exception object. Here we don’t need throw statement to throw an exception.

Example 01: JVM create and throw exception if JVM rule violates

Output :

main begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception21.throwkeyword.Example01ThrowKeyword.main(Example01ThrowKeyword.java:8)

In this example, Line :8 code violates one of the JVM rule(division by zero), So JVM itself creates and throws ArithmeticException object. Here we are not using ‘throw’ keyword to throw exception.

For every JVM rule violation, JVM has defined specific exception to throw. For Example, if you try to perform operation on null then it will throw NullPointerException, If you try to divide with zero it will throw ArithmeticException. Like wise for every violation JVM throws specific exception.

Example 02: JVM doesn’t create and throw exception for business rules violation

Output :

Before withdraw Balance : 1000
After withdraw Balance : -1000

In this example, At Line :17 we declared int accountBalance = 1000; Here int data type does allows even negative values also. That is the reason why at Line:21 even when we try to deduct more amount than available balance JVM didn’t throw exception. Because this is not JVM rule violation.

Here exactly we need to create and throw exception explicitly. Here we could make use of ‘throw‘ keyword to throw exception.

throw keyword

The Java throw keyword is used to explicitly throw an exception.

We can throw either checked or uncheked exception by throw keyword.

The throw keyword can be used to throw predefined exception or custom exception(user defined exception).

So throw keyword used to terminate the execution of block or method or application when our business rules are violated in application.

The syntax of java throw keyword is given below.

Let’s see the example of throw IllegalArgumentException.

If you don’t need reference of exception object then we can throw as follows.

Example 03: Create and throw predefined exception when business rule violated

Output :

Before withdraw Balance : 1000
Exception in thread “main” java.lang.IllegalArgumentException: Insufficient Funds!!!
at com.vidvaan.corejava.exception21.throwkeyword.BankAccount2.withDrawAmount(Example03ThrowKeyword.java:22)
at com.vidvaan.corejava.exception21.throwkeyword.Example03ThrowKeyword.main(Example03ThrowKeyword.java:9)

In the above example, At Line :22 if withDrawalAmount > accountBalance then we are creating and throwing IllegalArgumentException. So the program got terminated.

Good thing here is we are not allowing the withdrawal when there are no sufficient funds in the account.

Bad thing here is the complete program has terminated which is not acceptable. If certain block or method is violating business rules then we need to terminate only that particular block or method but not the whole application.

So we will rewrite above example to terminate certain code when business rule violated instead terminating whole application.

Example 04: Termination limiting to certain block of code when exception raised

Output :

Before withdraw Balance : 1000
Withdraw Failed : Insufficient Funds!!!
After withdraw Balance : 1000

In the above example, we can see that as business rule is violated not whole application is terminated only certain part of application terminated. This is the better way of handling exception.

Example 05: Create and throw user defined exception when business rule violated

Output :

Before withdraw Balance : 1000
Withdraw Failed : Insufficient Funds!!!
After withdraw Balance : 1000

In the above example, we defined user defined exception called ‘InsufficientFundsException’ to specify specific business rule violation. It is always recommended to define custom exception for every business rule violation instead of using predefined exceptions.

Example 06: throw ‘null’ leads to NullPointerException

Output :

Exception in thread “main” java.lang.NullPointerException: Cannot throw exception because “exception” is null
at com.vidvaan.corejava.exception21.throwkeyword.Example06ThrowKeyword.main(Example06ThrowKeyword.java:8)

Example 07: Statements immediately after ‘throw’ causes Compilation error

Unconditional throw statement should be the last statement in the block or method.

Example 08: Statements after ‘throw’ is allowed if ‘throw’ is conditional

Example 09: Throwing non Throwable types leads to Compilation error

In the above example we are trying to throw ‘String’ class object. But we should throw of type ‘Throwable’ or its subclass. Otherwise it leads to compilation error.

Example 10: Throwing checked exception leads to Compilation error

In the above example, we are throwing checked exception in doStuff() method. If a code throws unchecked exception, the compiler demand us to handle or declare exception. Otherwise we will get compilation error. Here explained how to handle checked exceptions.

Scroll to Top