Difference between throw and throws in Java Exception Handling
I am learning Java. I reached the topic of exception handling in Java. I am not able to understand the exact difference between throw and throws with respect to exception handling in Java. What is the actual difference between the two ? What are the funtionality of each individual ? I am totally lost in this topic.
Re: Difference between throw and throws in Java Exception Handling
The keyword throw and throws are used for Exception handling in Java. Sun Microsystems developed Java has many feature of which is Exception Handling. Precisely a throw statement initiates an exception to be thrown, where as the throws clause declares that the method is expected to throw one or more checked execptions. Thus both are different and perform two different task.
Re: Difference between throw and throws in Java Exception Handling
In very simple words, "throws" declares that the method is capable of throwing an exception while "throw" actually does the job of throwing the exception. They are not same.
eg:
Code:
public void Exep(int param) throws MyException
{
if (param > 7)
{
throw new MyException("You have entered big value");
}
.
.
.
}
Consider the above code. The keyword throws signifies that the method MyException is expected to throw exception and the keyword throw will actually throw the exception.
Re: Difference between throw and throws in Java Exception Handling
The keyword throw in Java is used to raise exception explicitly that means it is used when a user defined exception is raised.
The keyword throws in Java is a modifier of a method that is expected to generate and throw exception/s. Mostly methods which are capable of throwing an exception but not handling are specified with throws keyword.