2015-10-04 80 views
0
import static java.lang.System.*; 
import java.io.*; 

public class ExceptionDemo { 


    public static void main(String args[]) { 
     try { 
       int x = 5/0; 
     }finally { 

      System.out.print("exception "); 
     } 
    } 
} 

import static java.lang.System.*; 
import java.io.*; 

public class ExceptionDemo { 


    public static void main(String args[]) { 
     try { 
      throw new Exception(); 
     } finally { 
      System.out.print("exception "); 
     } 
    } 
} 
+1

第一个没有明确地抛出任何异常,而第二个没有,你没有以任何方式处理它(抓住它或进一步抛出) – Titus

+0

@JeroenVannevel不,它不。有一个未捕获的例外。 – BackSlash

+0

@BackSlash:没关系,我没有注意到Ideone自己自动添加它。 –

回答

0

你有两个选择:

要么捕获异常你扔:

public static void main(String args[]) { 
    try { 
     throw new Exception(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     System.out.print("exception "); 
    } 
} 

要么让你方法扔它:

public static void main(String args[]) throws Exception { 
    try { 
     throw new Exception(); 
    } finally { 
     System.out.print("exception "); 
    } 
} 

但是你必须处理它。我自己喜欢捕捉并直接处理它。

+0

谢谢@yassin hajaj –

+0

@piyushjoshi不客气。不要忘记标记为接受:)。 –

相关问题