日期:2014-05-20  浏览次数:20756 次

请问一下,我这个Test程序那里出错了?
Java code

public class Test{
    public static void main(String[] args){
        Test TT = new Test();
        try{
            //TT.num(-1);
        }catch (MyExceptions1 ME1){
            System.out.println(ME1.getMessage());
        }catch (MyExceptions2 ME2){
            System.out.println(ME2.getMessage());
        }
    }

    void num1(int i) throws MyException1{
        if(i < 0){
        throw new MyException1("数字太小;");
        }
    }
    
    void num2(int i) throws MyExceptions2{
        if(i > 0){
        throw new MyException2("数字太大;");
        }
    }
    
    
    class MyException1 extends Exception{
        //private int id = 1;
        public MyException1(String s){
            super("数字太小;");
        }
    }
        
    class MyException2 extends Exception{
        //private int id = 2;
        public MyException2(String s){
            super("数字太大;");
        }
    }     
}




总是报错,有人能帮我解决下么?

------解决方案--------------------
MyExceptions1和MyExceptions2 拼写错误
------解决方案--------------------
共有2个问题

public class Test{
public static void main(String[] args){
Test TT = new Test();
try{
//TT.num(-1);
}catch (MyExceptions1 ME1){
System.out.println(ME1.getMessage());
}catch (MyExceptions2 ME2){
System.out.println(ME2.getMessage());
}
catch 是处理异常的,你连异常都没有,干嘛catch ?
}

void num1(int i) throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyExceptions2你干嘛多写一个s{
if(i > 0){
throw new MyException2("数字太大;");
}
}


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}

------解决方案--------------------
你要的应该是

public class Test {
public static void main(String[] args){
Test TT = new Test();
try{
TT.num1(-1);
TT.num2(1);
}catch (MyException1 ME1){
System.out.println(ME1.getMessage());
}catch (MyException2 ME2){
System.out.println(ME2.getMessage());
}
}

void num1(int i) throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyException2{
if(i > 0){
throw new MyException2("数字太大;");
}
}


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}