import java.io.*;
public class MyException extends Exception{
MyException(String s)throws MyException{
if(s.equals("abc"))throw new MyException(s);
else System.out.println("您输入的字符串是:"+s);

}
/**
 * @param args
 * @throws MyException 
 */
public static void main(String[] args)throws IOException, MyException {
try{System.out.print("请输入一个字符串:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
MyException myexception=new MyException(str);
//myexception.test(Double.parseDouble(str));
}catch(MyException s){
System.out.println("error");
} }
}
我如果不输入abc的话一切都正常,但我 输入abc时就不是我想要的结果:出现这样的错误:
请输入一个字符串:abc
java.lang.StackOverflowError
Exception in thread "main"
高手指教!在线等待。

解决方案 »

  1.   

    MyException(String s)throws MyException{
    if(s.equals("abc")) {
    throw new MyException(s);
    }else{
    System.out.println("您输入的字符串是:"+s);
    }
    }我看了爆寒……你想想看你一旦输入了“abc”,这个函数会怎么做。
      

  2.   

    出现 StackOverflowError 是因为陷入了无休止的递归调用。
      

  3.   

    呵呵 throw new MyException("abc");...............throw new MyException("abc");