Java throws keyword

The Java throws keyword is used to declare an exception. It gives an information to the caller method that there is a chance to occur an exception in the execution of the method.

The main objective of throws keyword is to delegate the responsibility of exception handling to the caller method

  • We can declare both checked and unchecked exception and Errors
  • throws doesn’t really throw an exception, it is just a declaration of exception to the caller
  • We can declare multiple exceptions in any order with one throws keyword
  • We can declare mix of checked and unchecked exception in any order
  • In method declaration we are allowed to use only one throws keyword
  • throws is mandatory in method signature if method throw checked exception otherwise we will get compilation error
  • throws is optional in method signature if method throw unchecked exception
  • throws should declare only throwable types otherwise we will get compile time error saying incompatible types

Syntax of java throws

Example 01: throws keyword – declare unchecked exception

Output :

main() begin
Exception in thread “main” java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example01DeclareUncheckedException.doStuff(Example01DeclareUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example01DeclareUncheckedException.main(Example01DeclareUncheckedException.java:18)

In the above example, doStuff() method declares unchecked IllegalArgumentException. As it is unchecked exception the caller method main() not mandatory to handle it or declare it. As it is not handled any where in the call stack, program got terminated.

Example 02: throws keyword – caller handle unchecked exception

Output:

main() begin
java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example02CallerHandleUncheckedException.doStuff(Example02CallerHandleUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example02CallerHandleUncheckedException.main(Example02CallerHandleUncheckedException.java:19)
main() end

In the above example, doStuff() method declares unchecked IllegalArgumentException. Here caller main() method has coded to handle IllegalArgumentException. As exception handled, program successfully executed without abnormal termination.

Example 03: throws keyword – caller too declare unchecked exception

Output:

main() begin
Exception in thread “main” java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example03CallerDeclareUncheckedException.doStuff(Example03CallerDeclareUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example03CallerDeclareUncheckedException.main(Example03CallerDeclareUncheckedException.java:18)

In the above example, doStuff() method declares unchecked IllegalArgumentException. Here caller main() method also declared IllegalArgumentException instead handling it. As no one handles IllegalArgumentException program got terminated.

Whether it is checked, unchecked or error if we don’t handle it some where in its call stack, it will terminate the program

Always handle exception

Example 04: throws keyword – method declare checked exception

Compilation Error :

Unhandled exception type IOException

In the above example, doStuff() method declares checked IOException. We are getting compilation error in main() method at the line we are calling doStuff() method. As doStuff() method is declared checked exception, the rule is main() method either handle it or declare it. Otherwise program won’t get compiled.

If you are calling a method that declares checked exception, you must either caught or declare the exception.

Case1: Caller caught the exception i.e. handle the exception using try/catch.
Case2: Caller declare the exception i.e. specifying throws with the method.

Example 05: throws keyword – caller handle checked exception

Output :

main() begin
Something wrong !!!
main() end

In the above example, doStuff() method declares checked IOException. We are handling IOException in main() method.

Example 06: throws keyword – caller declare checked exception

Output :

main() begin
Exception in thread “main” java.io.IOException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example06CallerDeclareCheckedException.doStuff(Example06CallerDeclareCheckedException.java:11)
at com.vidvaan.corejava.exception22.throwskeyword.Example06CallerDeclareCheckedException.main(Example06CallerDeclareCheckedException.java:20)

In the above example, doStuff() method declares checked IOException. The caller method main() also declared IOException. As no one handles IOException, program got terminated. So we should always handle the exception some where in the application as per business need.

Example 07: throws keyword – declare error – catch error

Output :

main() begin
Something wrong !!!
main() end

In the above example, doStuff() method declares AssertionError. We are handling AssertionError in main() method.

In the case of ‘throws’ keyword Error classes also behave same like a unchecked exceptions.

We rarely declare Error classes in our regular programming practices. But it is allowed as shown above.

Scroll to Top