Kaa*_*reZ 2 java exception-handling exception
这个问题看起来很简单.如何在异常上继续运行我的应用程序.
例如,我将如何做到这一点.
服务器应用程序尝试在端口1234上运行,但它不可用.通常它会崩溃.但我怎么能让它继续运行并说出来; 你想尝试另一个港口吗?
或者,如果我们正在尝试加载不存在的文件.如何避免程序崩溃,只是显示一条消息说它无法加载文件.
这是怎么做到的?
使用try catch块.
try{
//Where exception may happen
}catch(Exception e){//Exception type. Exception covers it all.
//Print error if you would like or do something else
}finally{//Finally is optional, as the code in here will run regardless of an exception.
}
//program continues
Run Code Online (Sandbox Code Playgroud)
大多数try-catch块最后都没有finally.finally如果你需要代码来运行天气,你会告诉我们有一个例外. 有关finally块的更多信息
| 归档时间: |
|
| 查看次数: |
3176 次 |
| 最近记录: |