Last Updated: 2 December, 2023
In Java, the keywords 'throw' and 'throws' are often used in the context of exception handling. Both keywords have different functionality and uses in Java programs.
In order to develop robust Java code, it is important to understand these keywords, their functions, and their uses in Java programs. Going forward, we will also see the difference between both keywords.
In Java, the 'throw' keyword is used to explicitly throw an exception (a user-defined or built-in exception) to the JVM. 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 of "throw" keyword in Java
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)
Read more about 'throw' Keyword →
In Java, the 'throws' keyword is used in a method signature and declares which exceptions can be thrown from a method.
Exceptions can be propagated in the call stack using the 'throws' keyword. When a method declares that it throws an exception, it is not required to handle the exception. The caller of a method that throws exceptions is required to handle the exceptions (or throw them to its caller, and so on) so that the flow of the program can be maintained.
The 'throws' keyword enables us to declare multiple exceptions; if we do so, a comma should separate each exception in the declaration.
Throws only handle checked exceptions. Unchecked exceptions do not need to be thrown or handled explicitly in code.
Syntax:
Example of "throws" keyword
Output
Division Result: 5
ArrayIndexOutOfBounds Exception thrownIndex 5 out of bounds for length 2
Read more about 'throws' Keyword →
throw | throws |
The 'throw' keyword is used to throw an exception object explicitly. | The 'throws' keyword is used to declare an exception as well as bypass the caller. |
'throw' allows us to declare only one exception at a time. | 'throws' allow us to declare multiple exceptions at a time. |
'throw' is used inside the method body. | 'throws' is used with the method signature. |
'throw' is followed by an instance. | 'throws' is followed by a class name. |
Only unchecked exceptions can be propagated using the 'throw' keyword. | 'throws' keyword can be used to propagate checked exceptions only. |
Example: throw new StudentRecordNotFound(“Student record is not available.”); | Example: throws IOException, ArrayIndexBoundException; |
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com