打字高手v6.1:请高手写Java中异常的例子

来源:百度文库 编辑:中科新闻网 时间:2024/05/03 08:15:22
我在Java中学习异常,但是思路不太清晰
请哪位高手为我能写个关于异常的 简单点但是结构很清晰的例子
谢谢了

class TestException {
public static void throwException() throws RuntimeException
{
System.out.println("下面产生一个异常并将其抛出!");
throw new RuntimeException("MyTestException");
}
public static void main(String [] args)
{
try
{
throwException();
}
catch(RuntimeException re)
{
System.out.println("捕获异常:"+re);
}
}
}

异常在正常的控制流程中是可恢复的中断.所有的异常都从java.lang.Throwable类继承而来.