Java 编程/关键字/throws
外观
throws
是 Java 关键字。用于在方法定义中声明该方法抛出的异常。
语法
public myMethod() throws
MyException1, MyException2
{MyException1
...
}
示例
class MyDefinedException extends Exception
{
public MyDefinedException(String str)
{
super(str);
}
}
public class MyClass
{
public static void showMyName(String str) throws MyDefinedException
{
if(str.equals("What is your Name?"))
throw new MyDefinedException("My name is Blah Blah");
}
public static void main(String a[])
{
try
{
showMyName("What is your Name?");
}
catch(MyDefinedException mde)
{
mde.printStackTrace();
}
}
}
|