作者elenaser (九号)
看板java
标题Re: [问题] 超时的例外处理
时间Thu Sep 9 15:17:43 2010
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
1F:→ elenaser:FYI. 有误导请多指教. 看看是不是你想要达到的效果 09/09 15:18
2F:→ tkcn:建议你如果要贴程式码,最好重新排版并且只贴上重要部份 09/09 15:31
3F:→ elenaser:可以教我排版吗T.T 09/09 15:35