您的位置:首页 > 编程语言 > Java开发

Java中自定义异常的两种处理方法

2016-08-24 20:13 656 查看
其中注释掉的部分就是处理方法之一

class DivisorIsZeroException extends Exception
{
public DivisorIsZeroException(String errorMessage)
{
super(errorMessage);
}
}

class A
{
int divide(int a, int b) throws DivisorIsZeroException
{
//		try
//		{
//			if (0 == b)
//				throw new DivisorIsZeroException("除数不能为零!");
//		}
//		catch (DivisorIsZeroException e)
//		{
//			e.printStackTrace();
//		}

if (0 == b)
throw new DivisorIsZeroException("除数不能为零!");
int m = a / b;
return m;
}
}

public class TestA
{
public static void main(String[] args)
{
A aa = new A();
aa.divide(6, 0);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: