Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between throw and throws in Java? [duplicate]

Tags:

java

throw

throws

Can any one clearly state the difference between throw and throws in Java exception handling with an example? I have tried googling but couldn't arrive at a conclusion. Pls help

like image 658
user3527594 Avatar asked Sep 02 '14 07:09

user3527594


People also ask

What is the difference between throw and throws in Java?

The throw keyword is used to throw an exception explicitly. It can throw only one exception at a time. The throws keyword can be used to declare multiple exceptions, separated by a comma.

What's the difference between throw and threw?

Threw is the past tense of the verb throw. It's the word you use to say that something threw you for a loop or threw you off. Through is an adverb and a preposition.

What is the difference between throw and throws in Java Javatpoint?

Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code. Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.

Can we use throw and throws together in Java?

Basically throw and throws are used together in Java. Method flexibility is provided by the throws clause by throwing an exception. The throws clause must be used with checked exceptions. The throws clause is followed by the exception class names.


2 Answers

  1. throws clause is used to declare an exception and throw keyword is used to throw an exception explicitly.

  2. If we see syntax wise then throw is followed by an instance variable and throws is followed by exception class names.

  3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).

For example

throw

throw new Exception("You have some exception")
throw new IOException("Connection failed!!")

throws

public int myMethod() throws IOException, ArithmeticException, NullPointerException {}
  1. You cannot declare multiple exceptions with throw. You can declare multiple exception e.g. public void method()throws IOException,SQLException.

  2. checked exceptions can not be propagated with throw only because it is explicitly used to throw an particular exception. checked exception can be propagated with throws.

Exception propagation: An exception propagates from method to method, up the call stack, until it's caught. So if a() calls b(), which calls c(), which calls d(), and if d() throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception. what is exception propagation?

like image 179
Nirav Prajapati Avatar answered Oct 20 '22 13:10

Nirav Prajapati


throw use for throwing actual Exception and throws declare at method it might throws Exception.

public int findMax(int[] array) throws Exception{
    if(array==null)
       throw new NullPointerException(...);
    ...
}
like image 32
Subhrajyoti Majumder Avatar answered Oct 20 '22 14:10

Subhrajyoti Majumder