What is the difference between throw and throws?

Options
- The throws key word is used to explicitly throw an exception, while throw is utilized to handle checked exceptions
- The throw key word is used to implicitly throw an exception, while throws is utilized to handle checked exceptions
- The throws key word is used to implicitly throw an exception, while throw is utilized to handle checked exceptions
- The throw key word is used to explicitly throw an exception, while throws is utilized to handle checked exceptions


CORRECT ANSWER : The throw key word is used to explicitly throw an exception, while throws is utilized to handle checked exceptions

Discussion Board
Throw and Throws

The throw keyword is a special keyword used to explicitly throw an exception, whereas throws is also a special keyword and utilized to handle checked exceptions. The differences are as follows:

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

2. 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).

4. For example:

Throw:

....
static{
try {
throw new Exception("Something went wrong!!");
} catch (Exception exp) {
System.out.println("Error: "+exp.getMessage());
}
}
....
Throws:

public void sample() throws ArithmeticException{
//Statements

.....

//if (Condition : There is an error)
ArithmeticException exp = new ArithmeticException();
throw exp;
...
}
4. By using Throw keyword in java you cannot throw more than one exception but using throws you can declare multiple exceptions.

5. Throw is used for exceptions like a ArithmeticException and IOException whereas throws is used for IOException, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException

Rohit Sharma 08-12-2014 04:45 PM

hi

i think option 1 is correct for 16th bit

lmadhu 11-6-2013 01:13 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement