class ChushulingException extends Exception
{
public ChushulingException(String msg)
{
super(msg);
}
}
class ChushufuException extends Exception
{
public ChushufuException(String msg)
{
super(msg);
}
}
class Numbertest
{
public int shang(int x,int y) throws ChushulingException,ChushufuException
{
if(y<0)
{
throw new ChushufuException("您输入的是"+y+",规定除数不能为负数!");//抛出异常
}
if(y==0)
{
throw new ChushulingException("您输入的是"+y+",除数不能为0!");
}
int m=x/y;
return m;
}
}
class Rt001
{
public static void main(String[]args)
{
Numbertest n=new Numbertest();
//捕获异常
try
{
System.out.println("商="+n.shang(1,-3));
}
catch(ChushulingException yc)
{
System.out.println(yc.getMessage());
yc.printStackTrace();
}
catch(ChushufuException yx)
{
System.out.println(yx.getMessage());
yx.printStackTrace();
}
catch(Exception y)
{
System.out.println(y.getMessage());
y.printStackTrace();
}
finally{ System.out.println("finally!");} ////finally不管发没发生异常都会被执行
}
}
/*
class 异常类名 extends Exception
{
public 异常类名(String msg)
{
super(msg);
}
}
try{}
catch(异常类名 y){}
catch(异常类名 y){}
getMessage() //输出异常的信息 printStackTrace() //输出导致异常更为详细的信息
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有