+ 1
What's the main difference between throws and throw keyword in java ?
4 Answers
+ 4
The throws keyword is used to mark a method which will throw a exception. By doing this, the user must handle that exception. This lets them handle the exception in their own way.
The throw keyword raises that exception, i.e. causes the exception to happen.
Example:
public void myMethod() throws IOException {
try {
do some Input/output which may cause a IO exception.
} catch (IOException e) {
throw e;
}
}
public void myMethod2() {
try {
myMethod();
} catch (IOException e) {
Handle the error here.
}
}
+ 1
Kindly vote my question..
+ 1
exception that is thrown out of method is specified by throws.
to create exception intentionally inside member fun we use throw.