Re: [問題] 超時的例外處理
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
09/09 15:18, 1F
→
09/09 15:31, , 2F
09/09 15:31, 2F
→
09/09 15:35, , 3F
09/09 15:35, 3F
討論串 (同標題文章)