Java Programming/Keywords/throws
From Wikibooks, the open-content textbooks collection
throws is a Java keyword.
Used in a method definition to declare the Exceptions to be thrown by the method.
Syntax:
public myMethod() throws MyException1, MyException2
{
...
}
Example:
class MohanDefinedException extends Exception
{
public MohanDefinedException(String str)
{
super(str);
}
}
public class MohanClass
{
public static void showMyName(String str) throws MohanDefinedException
{
if(str.equals("What is your Name?"))
throw new MohanDefinedException("My name is Mohan Rajashekaran");
}
public static void main(String a[])
{
try
{
showMyName("What is your Name?");
}
catch(MohanDefinedException mde)
{
mde.printStackTrace();
}
}
}