public class MethodException
{
public static void main(String args[])
{
Test test=new Test(2,0);
try
{
test.sub(test.a,test.b);
}
catch(Exception x)
{
System.out.println("出现异常!");
}
}
}
class Test
{
int a,b;
public Test(int x,int y)
{
this.a=x;
this.b=y;
}
int sub(int x,int y) throws Exception
{
return x/y;
}
}
在指定方法中抛出异常,在调用该方法的地方处理异常
最新推荐文章于 2023-04-01 21:27:50 发布