Java编写

来源:百度知道 编辑:UC知道 时间:2024/06/16 16:55:48
编写一个ZeroException类,可以用来处理当除数为0时的异常。

public class Test {
public static void main(String[] args) throws ZeroException{
throw new ZeroException();
}
}
/**
* 除数为0异常
* @author Administrator
*
*/
class ZeroException extends Throwable{
ZeroException(){
System.out.println("除数不能为0");
}
ZeroException(String message){
super(message);
}
}

public class ZeroException extends Exception {

public ZeroException(String message){
super(message);
}
}

public class Test {

public static void main(String[] args) {
try {
Test.zero();
} catch (MyException e) {
e.printStackTrace();
}
}
public static void zero() throws MyException{
try{
int s = 5/0;
}catch(Exception e){
throw new MyException();
}
}

}

class MyException extends Exception{

public String toStri