SCJP題庫第219題

 
Given:
1. public class Threads2 implements Runnable{
2.
3.   public void run(){
4.     System.out.println("run.");
5.     throw new RuntimeException("Problem");
6.   }
7.   public static void main(String[] args){
8.     Thread t = new Thread(new Threads2());
9.     t.start();
10.   System.out.println("End of method.");
11. }
12.}
Which two can be results? (Choose two.)


A. java.lang.RuntimeException: Problem
B. run.
     java.lang.RuntimeException: Problem
C. End of method.
     java.lang.RuntimeException: Problem
D. End of method.
     run.
     java.lang.RuntimeException: Problem
E. run.
     java.lang.RuntimeException: Problem
     End of method.
Ans: DE

解說: main執行緒新建一條執行緒(第8行)後於第9行啟動此條執行緒,會有二個可能:
1.main執行緒先跑完,再執行該執行緒,參考答案D  2.該條執行緒先跑完再執行main,答案E。


throw new RuntimeException("Problem"); 
這條指令是丟出一個RuntimeException的例外物件,其中Problem是要顯示的例外訊息。

Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位