Last Updated: 11 October 2023
In Java, throw keyword is used to explicitly throw an exception (a user-defined or built-in exception) to the JVM. An exception is an object that represents an error or unexpected condition in a program. When an exception is thrown, the program's normal flow of execution is interrupted, and the exception is handled.
We can throw either checked or unchecked exceptions in Java by using the throw keyword.
When an exception is explicitly thrown, the program execution flow transfers from the try block to the catch block.
The throw keyword is used inside the method, and it is mainly used to throw custom (user-defined) exceptions. Using the throw keyword, only one exception-type object can be thrown at a time.
Syntax:
Example:
In the above example, we are throwing RecordNotFoundException explicitly; please note that when we are throwing custom exceptions, we make sure they are of type Throwable or a subclass of Throwable. For example, Exception is a subclass of Throwable, and the user-defined exceptions usually extend the Exception class.
As mentioned above, we can throw either checked or unchecked exceptions in Java by using the throw keyword. Let's understand this using the given examples below.
Example 1: Using "throw" keyword with Checked Exception
Output
java.io.FileNotFoundException
at com.javabytechie.throwkeyword.ThrowWithCheckedException.ReadStudentRecord(ThrowWithCheckedException.java:12)
at com.javabytechie.throwkeyword.ThrowWithCheckedException.main(ThrowWithCheckedException.java:17)
Thank you...
Example 2: Using "throw" keyword with Unchecked Exception
Output
Exception in thread "main" java.lang.ArithmeticException: ERROR: Invalid Roll Number at com.javabytechie.throwkeyword.ThrowWithUncheckedException.validateRollNumber(ThrowWithUncheckedException.java:7) at com.javabytechie.throwkeyword.ThrowWithUncheckedException.main(ThrowWithUncheckedException.java:15)
As of now, we have seen uses of the throw keyword with checked and unchecked exceptions; now let's see an example of the throw keyword with a custom (user-defined) exception, as given below.
Example 3: Using the "throw" keyword with Custom Exception
Output
com.javabytechie.throwkeyword.RollNumberNotFoundException: ERROR: Roll Number Not Found...
at com.javabytechie.throwkeyword.ThrowWithCustomException.validateRollNumber(ThrowWithCustomException.java:7)
at com.javabytechie.throwkeyword.ThrowWithCustomException.main(ThrowWithCustomException.java:15)
Thank you...
Rethrowing an exception was introduced in the Java 7 version. This feature allows us to explicitly re-throw the same exception from the catch block using the throw keyword. When the same exception object is re-thrown, it preserves the details of the original exception.
When to use: There might be a requirement where we want to catch an exception in the catch block and also want its caller to be notified about the exception. This is possible by rethrowing the exception using the throw statement.
Syntax
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com