当前位置: 代码迷 >> Java相关 >> [求助]再次求助
  详细解决方案

[求助]再次求助

热度:159   发布时间:2007-04-08 13:36:12.0
[求助]再次求助

import java.io.*;
class MyException extends Exception
{
MyException()
{
super();
}
MyException(String s)
{
super(s);
System.out.println("the Exception with ABCD");
}
}

public class exception2
{
public static void main(String args[])throws IOException
{
while(true)
{
try
{
fn();
}
catch(RuntimeException e)
{
try
{
throw e.getCause();//这个方法不对么
}
catch(IOException s)
{
System.out.println("please again");
}
catch(MyException m)
{
break;
}

}
}
}
static void fn()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("putout u String");
String temp=br.readLine();
if(temp.equals("ABCD")==false)
{
throw new IOException();
}
else if(temp.equals("ABCD")==true)
{
throw new MyException("ABCD");
}
}catch(Exception e)//把他包装成runtime的异常
{
throw new RuntimeException(e);
}

}
}

怎么解决,让异常正确识别其类型????

搜索更多相关的解决方案: super  void  public  import  

----------------解决方案--------------------------------------------------------
用instanceof
----------------解决方案--------------------------------------------------------
以下是引用Satyr在2007-4-8 13:36:12的发言:

import java.io.*;
class MyException extends Exception
{
MyException()
{
super();
}
MyException(String s)
{
super(s);
System.out.println("the Exception with ABCD");
}
}

public class exception2
{
public static void main(String args[])throws IOException
{
while(true)
{
try
{
fn();
}
catch(RuntimeException e)
{
try
{
throw e.getCause();//因为getCause返回的是个Throwable类型,所以必须对Throwable进行捕获
}
catch(IOException s)
{
System.out.println("please again");
}
catch(MyException m)
{
break;
}
catch(Throwable t) //加上这句
{
}

}
}
}
static void fn()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("putout u String");
String temp=br.readLine();
if(temp.equals("ABCD")==false)
{
throw new IOException();
}
else if(temp.equals("ABCD")==true)
{
throw new MyException("ABCD");
}
}catch(Exception e)//把他包装成runtime的异常
{
throw new RuntimeException(e);
}

}
}

怎么解决,让异常正确识别其类型????


----------------解决方案--------------------------------------------------------
try
{
if(e instanceof IOException)
throw new IOException();//这个方法不对么
else throw new MyException();
}
不行啊
----------------解决方案--------------------------------------------------------
2楼谢谢了
----------------解决方案--------------------------------------------------------
呵呵,我的方法不管用么?
----------------解决方案--------------------------------------------------------
说错了
3楼.............
----------------解决方案--------------------------------------------------------

汗,还以为我的方法不行呢


----------------解决方案--------------------------------------------------------
我们刚好上到这儿
----------------解决方案--------------------------------------------------------
  相关解决方案