废话不多说了,直接给大家贴代码了,具体代码如下所示:
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不管发没发生异常都会被执行
}
}
以上所述是小编给大家介绍的Java自定义异常_动力节点Java学院整理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



