这个是我关于异常的测试,为什么结果不是按如下顺序显示(有时候):
aaaaaaaaaaaaaaaaaaaannnnnnn
Exception in thread "main" java.lang.RuntimeException: 抛出运行时异常
at ItHeiMa.FangXin.throwRuntimeException(FangXin.java:35)
at ItHeiMa.FangXin.main(FangXin.java:16)
- public static void main(String[] args)
- {
- try {
- FileInputStream f=new FileInputStream("a.text");
- } catch (Exception e) {
- System.out.println("aaaaaaaaaaaaaaaaaaaannnnnnn");
- }
-
- throwRuntimeException(10);
- throwCheckedException(10);
- }
-
- private static void throwCheckedException(int a)
- {
- if(a>9)
- try {
- throw new Exception("抛出检查异常");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- private static void throwRuntimeException(int a)
- {
- if(a>9)
- throw new RuntimeException("抛出运行时异常");
- }
复制代码
多运行几次,结果可能如下:
Exception in thread "main" aaaaaaaaaaaaaaaaaaaannnnnnn
java.lang.RuntimeException: 抛出运行时异常
at ItHeiMa.FangXin.throwRuntimeException(FangXin.java:35)
at ItHeiMa.FangXin.main(FangXin.java:16)
或者如下:
Exception in thread "main" java.lang.RuntimeException: 抛出运行时异常
at ItHeiMa.FangXin.throwRuntimeException(FangXin.java:35)
at ItHeiMa.FangXin.main(FangXin.java:16)
aaaaaaaaaaaaaaaaaaaannnnnnn
Main方法不是单线程么?可每次运行结果为什么会不一样,求大神解释,谢谢!
C:\Documents and Settings\user\桌面 |
|