class Excep
{
public double division(double a,double b)//throws ArithmeticException
{
return a/b;
}
}
class Myexception extends Exception
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
System.out.println(excep.division(3,0));
}
catch(Myexception e)
{
System.out.println(e);
}
}
}
{
public double division(double a,double b)//throws ArithmeticException
{
return a/b;
}
}
class Myexception extends Exception
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
System.out.println(excep.division(3,0));
}
catch(Myexception e)
{
System.out.println(e);
}
}
}
解决方案 »
- 错误java.lang.NumberFormatException: For input string: ""
- 读,删Sql2005数据库的两个语句??
- 100分求解多线程方面的问题(笔试时遇到的)
- 小弟弟的初级问题,如何解决?
- Exception in thread "main" java.lang.NoClassDefFoundError: java4 这是什么错误阿,以前一直好好的今天突然这样了
- 初学者的问题:我想将JBuilder包含的类导出来用(不用JBulider进行开发),我怎样才能最快地找到指定的类呢?谢谢!
- 请问如何边历一个hashmap啊?
- 急需微软的Visual SourceSafe(VSS)
- 请问:有没有对数组(int)进行排序的的方法???
- 对Interface的疑问
- 提高SQL性能另类基础方法 简单有效
- 如何在swing中将几个JTextField、JLabel、JButton放在一块。
class Myexception extends Exception
{
public Myexception()
{
super("除数为零");
}
public Myexception(String message)
{
super(message);
}
}
你的division方法没有声明抛出Myexception,所以下面catch它,会编译出错2.double的相除,不会异常。class Excep
{
public int division(int a,int b) throws Myexception
{
int result=0;
try {
result = a/b;
} catch (ArithmeticException e) {
throw new Myexception();
}
return result;
}
}
class Myexception extends Exception
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
int d = excep.division(3,0);
System.out.print(d);
} catch(Myexception e)
{
e.printStackTrace();
}
}
}
你的算术运算没有抛出异常 故捕获不到,从而使结果为Infinity.
而测试方法同时也是得事先声明异常的,要不无法抛出,也就无法编译找到.
还有楼上说的,double 默认为0.0 ,所以不会
class Excep{
public double division(int a,int b)throws Myexception{
if(b==0)
throw new Myexception();
else
return a/b;
}
}class Myexception extends Exception{
public Myexception(){
super("除数为零");
}
}public class ExceptionTest{
public static void main(String[] args)throws Myexception{
Excep excep =new Excep();
try{
System.out.println(excep.division(3,0));
}catch(Myexception e){
System.out.println(e);
}
}
}
{
public int division(int a,int b) throws Myexception
{
int result=0;
try {
result = a/b;
} catch (ArithmeticException e) {//这里为什么是catch (ArithmeticException e)而不是catch (Myexception e)
throw new Myexception();
}
return result;
}
}
class Myexception extends Exception
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
int d = excep.division(3,0);
System.out.print(d);
} catch(Myexception e)
{
e.printStackTrace();
}
}
}
如果你继承ArithmeticException,应该可以.
因为编译器无法判断是否能够捕获算术异常.class Excep
{
public int division(int a,int b) throws Myexception
{
int result=0;
try {
result = a/b;
} catch (Myexception e) {//这里为什么是catch (ArithmeticException e)而不是catch (Myexception e)
System.out.println(e);
}
return result;
}
}
class Myexception extends ArithmeticException //这里继承ArithmeticException
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
int d = excep.division(3,0);
System.out.print(d);
} catch(Myexception e)
{
e.printStackTrace();
}
}
}
{
public int division(int a,int b) throws Myexception
{
int result=0;
try {
result = a/b;
} catch (Myexception e) {//这里为什么是catch (ArithmeticException e)而不是catch (Myexception e)
System.out.println(e);
}
return result;
}
}
class Myexception extends ArithmeticException //这里继承ArithmeticException
{
public Myexception()
{
super("除数为零");
}
}public class ExceptionTest
{
public static void main(String[] args)throws Myexception
{
Excep excep =new Excep();
try
{
int d = excep.division(3,0);
System.out.print(d);
} catch(Myexception e)
{
e.printStackTrace();
}
}
}
9楼的才是正解
4楼的是投机取巧的办法,按4楼的代码,只要是算数异常都会被抛成你的自定义异常!但是算数异常也有可能不是除0引起的呢???
是会被抛文件未找到异常。
为什么呢,java定义了文件未找到异常,然后在new FileOutputStream(String path )的构造方法中做了处理:
他用一个File类指向path ,然后调用他的 isexist()方法,如果不存在 ,手动throws文件未找到异常。
所以被你上层的程序catch住了,所以9楼的才是正解
我再说一下我的理解吧,上星期又看了一遍异常楼主你在ExceptionTest的main方法中try 里System.out.println(excep.division(3,0));捕获它的异常,而该句异常你认为会在division()方法中抛出
,可是在该方法中,直接返回了a/b,这时会抛出系统定义的ArithmeticException,而不是你定义的异常,因为你还是没有避免去抛出该系统异常,即使你在方法声明出加上throws ArithmeticException,那也仅仅表明你这个方法有可能抛出异常,真正在方法中抛异常还是得去用 throw 这个关键字来new一个你定义的异常抛出的。既然要抛自己的异常,你得确定出现什么情况抛啊,对本题,就是除数为0是抛,所以加上判断b是否为0.
public double division(double a,double b)//throws ArithmeticException
{
return a/b;
} 你函数没抛异常怎么可能捕捉到?异常不是凭空出现的,是要手动抛的
public Myexception(String message){
super(message);
}
}
按逻辑上说,应该是判断b==0才throw myException,在4楼只是为了说明异常的抛出方法
{
if(b == 0)
throw new Myexception();
return a/b;
}
抛异常的意义在于,告诉上一层有异常,要上一层根据自己的情况作出相应处理。下层当然就要帮上层做好判断!那些说4楼是对的的,完全是在误导楼主。