public class Main02 {
static void ceshi(int i){
if (i==1) System.out.println(i);
else{
throw new RuntimeException("运行时异常");
}
System.out.println("方法结尾");
}
public static void main(String[] args) {
ceshi(12);
// try{
//
// }catch (Exception e){
// e.printStackTrace();
// }
System.out.println("出现异常后,程序继续执行!");
}
}
结果
很显然,没有捕捉异常的时候,程序直接停止运行了
public class Main02 {
static void ceshi(int i){
if (i==1) System.out.println(i);
else{
throw new RuntimeException("运行时异常");
}
System.out.println("方法结尾");
}
public static void main(String[] args) {
try{
ceshi(12);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("程序是否继续执行?");
}
}
运行结果
很明显程序继续执行了,而且程序结束之后的返回code 为0.
public class Main02 {
static void ceshi(int i) throws Exception{
if (i==1) System.out.println(i);
else{
try{
throw new RuntimeException("运行时异常");
}catch (RuntimeException e){
e.printStackTrace();
System.out.println("方法内自己处理了异常");
}
}
System.out.println("方法结尾");
}
public static void main(String[] args) {
try{
ceshi(12);
}catch (Exception e){
System.out.println("异常从方法中抛出");
e.printStackTrace();
}
System.out.println("程序是否继续执行?");
}
}