此文只作为个人测试记录,对他人无实际参考意义!
1、程序异常or不异常
public class ExceptionTest {
public static void test(){
int a = 1 / 0;
System.out.println("test success!");
}
public static void main(String[] args) {
test();
}
}
输出:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.example.demo.filter.ExceptionTest.test(ExceptionTest.java:5)
at com.example.demo.filter.ExceptionTest.main(ExceptionTest.java:10)
结论:程序出现异常后,System.out输出语句不执行
若int a = 1 / 0;换成 int a = 1 / 1;则正常输出test success!
2、进行try catch
public class ExceptionTest {
public static void test(){
try{
int a = 1 / 0;
}catch (Exception e){
System.out.println("try catch success!");
}
System.out.println("test success!");
}
public static void main(String[] args) {
test();
}
}
输出:
try catch success!
test success!
结论:正常catch后,程序会执行catch里面的语句,并接着往下执行!
若int a = 1 / 0;换成 int a = 1 / 1;则无异常,catch中的语句不会执行,只输出
test success!
3、return + try catch
1)在try异常语句块后return;(在异常语句前return编译器检查不通过)
public class ExceptionTest {
public static void test(){
try{
int a = 1 / 0;
return;
}catch (Exception e){
System.out.println("try catch success!");
}
System.out.println("test success!");
}
public static void main(String[] args) {
test();
}
}
输出:
try catch success!
test success!
public class ExceptionTest {
public static void test(){
try{
int a = 1 / 1;
return;
}catch (Exception e){
System.out.println("try catch success!");
}
System.out.println("test success!");
}
public static void main(String[] args) {
test();
}
}
无输出
结论:try中异常语句后的return不会执行,try异常被catch后会接着执行catch语句块中的语句及之后的语句;如果try中无异常且return后,catch语句块和之后的就不会执行。
加上finally试试:
public class ExceptionTest { public static void test(){ try{ int a = 1 / 1; return; }catch (Exception e){ System.out.println("try catch success!"); //return; }finally{ System.out.println("finally success!"); } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:finally success!
public class ExceptionTest { public static void test(){ try{ int a = 1 / 0; return; }catch (Exception e){ System.out.println("try catch success!"); //return; }finally{ System.out.println("finally success!"); } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:
try catch success!
finally success!
test success!
结论:try中return前,finally中的语句还是会执行的。
2) 只有catch中return
public class ExceptionTest { public static void test(){ try{ int a = 1 / 0; return; }catch (Exception e){ System.out.println("try catch success!"); return; } System.out.println("test success!");//两个return后此句会检查出错 } public static void main(String[] args) { test(); } }
public class ExceptionTest { public static void test(){ try{ int a = 1 / 0; //return; }catch (Exception e){ System.out.println("try catch success!"); return; } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:try catch success!
public class ExceptionTest { public static void test(){ try{ int a = 1 / 1; //return; }catch (Exception e){ System.out.println("try catch success!"); return; } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:test success!
结论:若异常,catch中return后不执行后续语句;若无异常,则不执行catch中的语句。
PS:加上finally试试。
public class ExceptionTest { public static void test(){ try{ int a = 1 / 0; //return; }catch (Exception e){ System.out.println("try catch success!"); return; }finally{ System.out.println("finally success!"); } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:try catch success!
finally success!
public class ExceptionTest { public static void test(){ try{ int a = 1 / 1; //return; }catch (Exception e){ System.out.println("try catch success!"); return; }finally{ System.out.println("finally success!"); } System.out.println("test success!"); } public static void main(String[] args) { test(); } }
输出:finally success!
test success!
结论自己体会!!!
3、finally中return
public class ExceptionTest {
public static void test(){
try{
int a = 1 / 0;
//return;
}catch (Exception e){
System.out.println("try catch success!");
//return;
}finally{
System.out.println("finally success!");
return;
}
System.out.println("test success!");//编译器检查不通过
}
public static void main(String[] args) {
test();
}
}
注释后,其它几种情况同catch中return
结论:见上!try、catch、finally中都return,以finally中的为准!
4、带值return
public class ExceptionTest { public static String test(){ String str = "fsyy"; try{ int a = 1 / 0; return str + "try"; }catch (Exception e){ //System.out.println("try catch success!"); return str + "catch"; }finally{ //System.out.println("finally success!"); return str + "finally"; } //System.out.println("test success!"); } public static void main(String[] args) { System.out.println(test()); } }
输出:fsyyfinally
public class ExceptionTest { public static String test(){ String str = "fsyy"; try{ int a = 1 / 0; return str = str + "try"; }catch (Exception e){ //System.out.println("try catch success!"); return str = str + "catch"; }finally{ //System.out.println("finally success!"); return str = str + "finally"; } //System.out.println("test success!"); } public static void main(String[] args) { System.out.println(test()); } }
输出:fsyycatchfinally
public class ExceptionTest {
public static String test(){
String str = "fsyy";
try{
int a = 1 / 1;
return str = str + "try";
}catch (Exception e){
//System.out.println("try catch success!");
return str = str + "catch";
}finally{
//System.out.println("finally success!");
return str = str + "finally";
}
//System.out.println("test success!");
}
public static void main(String[] args) {
System.out.println(test());
}
}
输出:fsyytryfinally
结论:finally最后return。
5、奇怪的知识,暂未发现原理
public class ExceptionTest {
public static String test(){
String str = "fsyy";
try{
int a = 1 / 0;
return str = str + "try";
}catch (Exception e){
//System.out.println("try catch success!");
//return str = str + "catch";
str = str + "catch";
}finally{
//System.out.println("finally success!");
//return str = str + "finally";
str = str + "finally";
}
//System.out.println("test success!");
return str = str + "end";
}
public static void main(String[] args) {
System.out.println(test());
}
}
输出:fsyycatchfinallyend
但是当无异常时,输出fsyytry,且语句最后有return,catch和finally中都不能带return。
public class ExceptionTest {
public static String test(){
String str = "fsyy";
try{
int a = 1 / 1;
return str = str + "try";
}catch (Exception e){
//System.out.println("try catch success!");
//return str = str + "catch";
str = str + "catch";
}finally{
//System.out.println("finally success!");
//return str = str + "finally";
str = str + "finally";
}
//System.out.println("test success!");
return str = str + "end";
}
public static void main(String[] args) {
System.out.println(test());
}
}
输出:fsyytry
猜测结论是:无异常时try中return后会屏蔽语句最后的return