未抛出异常,帮忙看下哪里出问题了吧class DevideByMinusException extends Exception{
int devisor;
public DevideByMinusException(String msg,int devisor){
super(msg);
this.devisor=devisor;
}public int getDevisor(){
return devisor;
}
}
class Test{
Test(){}
public int devide(int x,int y) throws ArithmeticException,DevideByMinusException{
System.out.println("1");
if (y<0){
System.out.println(y);
throw new DevideByMinusException("被除数为负数",y);
}
System.out.println("5");
int result=x/y;
System.out.println("3");
return x/y;
}
}public class TestExeption{
public static void main(String[] args) {
try{
//int result=new Test().devide(3,0);
//int result=new Test().devide(3,-1);
Test t=new Test();
int r=t.devide(3,-1);
//int result=new Test().devide(3,1);
System.out.println("The result is"+r);
}catch (DevideByMinusException e){
e.getMessage();
e.getDevisor();
}catch(ArithmeticException e){
e.getMessage();
}catch(Exception e){
e.getMessage();
}
}
}
int devisor;
public DevideByMinusException(String msg,int devisor){
super(msg);
this.devisor=devisor;
}public int getDevisor(){
return devisor;
}
}
class Test{
Test(){}
public int devide(int x,int y) throws ArithmeticException,DevideByMinusException{
System.out.println("1");
if (y<0){
System.out.println(y);
throw new DevideByMinusException("被除数为负数",y);
}
System.out.println("5");
int result=x/y;
System.out.println("3");
return x/y;
}
}public class TestExeption{
public static void main(String[] args) {
try{
//int result=new Test().devide(3,0);
//int result=new Test().devide(3,-1);
Test t=new Test();
int r=t.devide(3,-1);
//int result=new Test().devide(3,1);
System.out.println("The result is"+r);
}catch (DevideByMinusException e){
e.getMessage();
e.getDevisor();
}catch(ArithmeticException e){
e.getMessage();
}catch(Exception e){
e.getMessage();
}
}
}
解决方案 »
- 本人新手,请教一个问题(这是我第一个真正意义上的游戏啊)
- 这个句子是对的吗?
- java怎样随机选取一个集合里面的数.
- 语句对吗?
- 关于正则表达式的问题
- String在Main方法中定义的问题
- 如何使用Swing 截取指定屏幕大小并转换为图片存取?不监听键盘(可以监听butten或者其他组件)。
- java中一个float对象占多大内存空间?
- 谁有IBM WebSphere Studio Application v5的英文版的下载网址和解密方法,兄弟定高分相送!
- 请教组件技术高手!欢迎讨论
- JCreator 3.5中的CVS如何使用呀,请高手指点一下!!!
- 通过javac编译,但不能用java运行.帮帮我啊!!!!!!
int devisor;
public DevideByMinusException(String msg,int devisor){
super(msg);
this.devisor=devisor;
}public int getDevisor(){
return devisor;
}
}class Test{ Test(){} public int devide(int x,int y) throws ArithmeticException,DevideByMinusException{
System.out.println("1");
//throw new DevideByMinusException("被除数为负数",y);
//throw new NullPointerException();
if (y<0)
{
System.out.println("ere" + y);
throw new DevideByMinusException("被除数为负数",y);
}
System.out.println("5");
int result=x/y;
System.out.println("3");
return x/y;
}
} class TestExeption{
public static void main(String[] args) {
try{
//int result=new Test().devide(3,0);
//int result=new Test().devide(3,-1);
Test t=new Test();
int r=t.devide(3,-1);
//int result=new Test().devide(3,1);
System.out.println("The result is"+r);
}catch (DevideByMinusException e){
System.out.println(e.getMessage());
e.getDevisor();
}catch(ArithmeticException e){
e.getMessage();
}catch(Exception e){
e.getMessage();
}
}
}你的异常已经被抛出了,只是你没有打印出来,在catch 中要用System.out.println();