The keyword throws is used to declare an exception from a method or constructor and defined with an instance of the Exception class.
It helps the programmer to predict that an exception may occur so it is better to write the exception handling code to maintain the normal flow of the code. There may be multiple exceptions written after the throws keyword.
return_type method_name() throws exception_class_name{
//code block
}
To throw an unchecked exception from a method, it is not necessary to handle the exception or declare it in the throws clause.
To throw a checked exception using throw, we must either handle the exception in catch block or method explicitly and declare it using throws declaration.
throw Instance
type methodName(parameters) throws exceptionList
exception handled
Knowledge2life
|