Difference Between Throw And Throws In Java (With Examples)

SHARE

Throw

The Java throw keyword is used to explicitly throw an exception.  In Java, exceptions can be categorized into two types:

  • Unchecked Exceptions: They are not checked at compile-time but at run-time.
  • Checked Exceptions: they are checked at compile-time.

Usually, we don’t need to handle unchecked exceptions. It’s because unchecked exceptions occur due to programming errors. And , it is a good practice to correct them instead of handling them.

We can throw either checked or unchecked exception in Java by throw keyword. The throw keyword is mainly used to throw custom exception.

What You Need To Know About  Throw In Java

  • Throw keyword can be used in switch case in Java.
  • Throw keyword is used to throw an exception explicitly from any method or static block.
  • Throw keyword is followed by an instance of throwable class or one of its sub-classes.
  • Using throw keyword you can also break a switch statement or a loop without using break keyword.
  • Throw is used within a method definition.
  • You cannot throw multiple exceptions using throw.
  • Throw statement will create exception object.
  • With throw keyword we can propagate only unchecked exception i.e checked exception cannot be propagated using throw.
  • Syntax wise throw keyword is followed by the instance variable.
  • Throw keyword is applicable only for Throwable objects.

Throws

Throws is a keyword in Java which is used in the signature of method to indicate that this method might throw one of the listed type exceptions. The caller to these methods has to handle the exception using a try-catch block.

If you do not handle the exception in a try catch block, compiling will fail. But almost every other method in the Java library or even user defined may throw an exception or two. Handling all the exceptions using the try and catch block could be cumbersome and will hinder the coder’s throughput.

What You Need To Know About Throws In Java

  • Throws keyword cannot be used anywhere except on method declaration line.
  • Throws keyword is used to declare an exception possible during its execution.
  • Throws keyword is followed by one or more Exception class names separated by commas.
  • Using throws keyword you cannot break a switch statement or a loop without using break keyword.
  • Throws is used within the method signature to declare an exception which might get thrown by the function while executing the code.
  • You can declare multiple exception using throws one of which may or may not throw by method.
  • Throws statement will not create exception object.
  • With throws keyword, both checked and unchaked exceptions can be declared. For the propagation checked exception must use throw keyword followed by specific exception class name.
  • Syntax wise throws keyword is followed by exception class names.
  • Throws keyword does not prevent abnormal termination of the program.

Also Read: Difference Between While And DO-While LOOP In Java

Difference Between Throw And Throws In Tabular Form

THROW THROWS
Throw keyword can be used in switch case in Java.   Throws keyword cannot be used anywhere except on method declaration line.  
Throw keyword is used to throw an exception explicitly from any method or static block.   Throws keyword is used to declare an exception possible during its execution.  
Throw keyword is followed by an instance of throwable class or one of its sub-classes.   Throws keyword is followed by one or more Exception class names separated by commas.  
Using throw keyword you can also break a switch statement or a loop without using break keyword.   Using throws keyword you cannot break a switch statement or a loop without using break keyword.  
Throw is used within a method definition.   Throws is used within the method signature to declare an exception which might get thrown by the function while executing the code.  
You cannot throw multiple exceptions using throw.   You can declare multiple exception using throws one of which may or may not throw by method.  
Throw statement will create exception object.   Throws statement will not create exception object.  
With throw keyword we can propagate only unchecked exception i.e checked exception cannot be propagated using throw.   With throws keyword, both checked and unchaked exceptions can be declared. For the propagation checked exception must use throw keyword followed by specific exception class name.  
Syntax wise throw keyword is followed by the instance variable.   Syntax wise throws keyword is followed by exception class names.  
Throw keyword is applicable only for Throwable objects.   Throws keyword does not prevent abnormal termination of the program.