Re: [問題] 超時的例外處理

看板java作者 (九號)時間15年前 (2010/09/09 15:17), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串2/2 (看更多)
import java.util.*; public class S60 { public static void main(String [] avgs) { threadCount threadExample=new threadCount(10);//建立物件 並傳入參數 threadExample.Start();//開始執行緒 } } class threadCount extends Thread { int sec; threadCount(int sec) { this.sec=sec;//傳入需要多少秒跳出的參數指定 } public void run() { try { for(int i=1;i<=sec;i++) {//計次迴圈 //System.out.print(i);//測試用看幾秒 Thread.sleep(1000);//執行一次停一秒 if(i>=sec) { throw new ArithmeticException();//超過時間拋出例外 } } } Catch(ArithmeticException e) { System.out.print("例外");//捕捉設定的例外 } Catch(Exception e) { System.out.print("執行緒掛了");//捕捉Sleep的執行緒有無 掛點 } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.250.108.16

09/09 15:18, , 1F
FYI. 有誤導請多指教. 看看是不是你想要達到的效果
09/09 15:18, 1F

09/09 15:31, , 2F
建議你如果要貼程式碼,最好重新排版並且只貼上重要部份
09/09 15:31, 2F

09/09 15:35, , 3F
可以教我排版嗎T.T
09/09 15:35, 3F
文章代碼(AID): #1CY8eQso (java)
文章代碼(AID): #1CY8eQso (java)