到底我哪里定义错了, 自定义异常, 哪里错了?

ioio88 2005-02-12 08:16:54

public class TestException
{
public static void main(String[] args)
{
try
{
int result = new Test().devide( 3, 0 );
//int result = new Test().devide( 3, -1 );
//int result = new Test().devide( 3, 1 );
System.out.println("the result is " + result );
}
catch(DevideByMinusException e)
{
System.out.println("program is running into"+
"DevideByMinusException");
System.out.println(e.getMessage());
System.out.println("the devisor is " +
e. getDevisor());
}
catch(ArithmeticException e)
{
System.out.println("program is running into"+
"DevideByMinusException");
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("program is running into"+
"other unknowned Exception");
System.out.println(e.getMessage());
}
System.out.println("program is running here ,that is normal !");
}
}
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
star_str 2005-02-13
  • 打赏
  • 举报
回复
这个一定在前面定义过,而在这里使用!

如果你已经确定会发生异常,那我觉得代码没有问题
hxzg001 2005-02-13
  • 打赏
  • 举报
回复
catch(DevideByMinusException e) 这里自定义异常的名称写错了吧应该是
catch(DivideByMinusException e)
ioio88 2005-02-13
  • 打赏
  • 举报
回复
回star_str(生命火花): 这是张孝祥<<Java就业培训教程>>P154页的内容, 全在这里了.
quickpoint 2005-02-12
  • 打赏
  • 举报
回复
/*
@(#) DivideByMinusException.java
*/

package com.quickpoint;
/**
DivideByMinusException is a self defined exception class.
*/
public class DivideByMinusException extends Exception
{
/**
exception msg String;
*/
private String msg;
/**
Constructor
*/
public DivideByMinusException(String msg)
{
this.msg = msg;
}
/**
Override toString method
@return string representation of the exception
*/
public String toString()
{
return msg;
}
/**
your divider code here...
*/

public String getDivider()
{
// ...
}
}
samwise2 2005-02-12
  • 打赏
  • 举报
回复
你的DevideByMinusException可能写的不对,我猜是没有继承ArithmeticException
你看我下面写你是不是你想得到的

/*
* Created on 2005-2-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.ess.test;

/**
* @author zhang
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DevideByMinusException extends ArithmeticException {

/**
* @return
*/
public String getDevisor() {

return null;
}

}



package com.ess.test;

public class TestException {
public static void main(String[] args) {
try {
int result = devide(3, 0);
// int result = new Test().devide( 3, -1 );
// int result = new Test().devide( 3, 1 );
System.out.println("the result is " + result);
} catch (DevideByMinusException e) {
System.out.println("the devisor is " + e.getDevisor());
}
System.out.println("program is running here ,that is normal !");
}

/**
* @param i
* @param j
* @return
*/
private static int devide(int i, int j) {
if(j==0)
throw new DevideByMinusException();
return i/j;
}
}
star_str 2005-02-12
  • 打赏
  • 举报
回复
devide是怎么定义的,能发上来看看吗

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧