你的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; } }
/* @(#) 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() { // ... } }
你看我下面写你是不是你想得到的/*
* 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;
}
}
@(#) 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()
{
// ...
}
}
catch(DivideByMinusException e)