try
{
System.out.println(a/b);
}
这个代码是有问题的,你自定义了一个异常类,要在a/b时判断并抛出。我给你写一个,你参考一下
class Get{
//代码的优化,尽量让一个代码处理一件事情。
public static int method(int a,int b)throws AException{
if(b==0)//在哪有错误,在哪抛出
throw new AException("除零了");
else{return a/b;}
}
}
public class ExceptionTest5{
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println(Get.method(1, 0));
}
catch (AException e)
{
e.printStackTrace();
}
}
}
class AException extends RuntimeException
{
AException(String msg)
{
super(msg);
}
}
感觉你进入了一个误区,不是你自定义完了,他就能自行抛出的,自己好好琢磨琢磨吧 。 |