java试验5 异常的抛出、捕获并处理

来源:百度知道 编辑:UC知道 时间:2024/06/29 02:01:50

public class ExceptionTest {
public static void main(String[] args ) {
ExceptionTest et = new ExceptionTest();
try {
et.GetException();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

private void GetException() throws Exception {
throw new Exception("这是一个测试, 不要惊慌, 程序目前运行良好!");
}
}

try{
//可能会发生异常的代码
}catch(/* 要捕获的异常类型 */){
//捕获到异常之后执行的代码
}finally{
//最后一定会执行的代码(不管是否有异常)
}