10 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

  1. Throw keyword can be used in switch case in Java.
  2. Throw keyword is used to throw an exception explicitly from any method or static block.
  3. Throw keyword is followed by an instance of throwable class or one of its sub-classes.
  4. Using throw keyword you can also break a switch statement or a loop without using break keyword.
  5. Throw is used within a method definition.
  6. You cannot throw multiple exceptions using throw.
  7. Throw statement will create exception object.
  8. With throw keyword we can propagate only unchecked exception i.e checked exception cannot be propagated using throw.
  9. Syntax wise throw keyword is followed by the instance variable.
  10. 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

  1. Throws keyword cannot be used anywhere except on method declaration line.
  2. Throws keyword is used to declare an exception possible during its execution.
  3. Throws keyword is followed by one or more Exception class names separated by commas.
  4. Using throws keyword you cannot break a switch statement or a loop without using break keyword.
  5. Throws is used within the method signature to declare an exception which might get thrown by the function while executing the code.
  6. You can declare multiple exception using throws one of which may or may not throw by method.
  7. Throws statement will not create exception object.
  8. 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.
  9. Syntax wise throws keyword is followed by exception class names.
  10. 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.