不加try catch
package com.it.test;
public class TryCatchTest {
public static void main(String[] args) {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry(){
int number = 0;
int numberReturn = 1;
numberReturn = 10 / number;
System.out.println("报错了");
return numberReturn;
}
}
结果
加了try catch
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry(){
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
}catch (Exception e){
e.getStackTrace();
}
System.out.println("报错了");
return numberReturn;
}
}
结果
catch里有代码
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry(){
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
}catch (Exception e){
numberReturn = 2; //对numberReturn再次赋值
System.out.println("catch里的代码走了");
e.getStackTrace();
}
System.out.println("报错了");
return numberReturn;
}
}
结果
catch里再throw 一个Exception
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) throws Exception {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry() throws Exception {
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
} catch (Exception e){
numberReturn = 3;
throw new Exception();
// System.out.println("catch里的代码走了");//在catch后的throw里不能再加代码了,idea会自动检测unreachable statement
}
System.out.println("报错了");
return numberReturn;
}
}
结果
catch里有return====直接在catch的return里返回
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) throws Exception {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry() throws Exception {
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
} catch (Exception e){
numberReturn = 3;
return numberReturn;
}
System.out.println("报错了");
numberReturn = 4;
return numberReturn;
}
}
结果
try catch finally
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) throws Exception {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry() throws Exception {
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
} catch (Exception e){
numberReturn = 3;
} finally {
numberReturn = 4;
System.out.println("finally代码被执行");
}
numberReturn = 5;
return numberReturn;
}
}
结果
catch里有return,finally里没有
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) throws Exception {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry() throws Exception {
int number = 0;
int numberReturn = 1;
try{
numberReturn = 10 / number;
} catch (Exception e){
numberReturn = 3;
return numberReturn;
} finally {
numberReturn = 4;
System.out.println("finally代码被执行");
}
numberReturn = 5;
return numberReturn;
}
}
结果
catch和finally里都有return
package main.java.test;
public class TryCatchTest {
public static void main(String[] args) throws Exception {
int i = testTry();
System.out.println("值为" + i);
}
public static int testTry() throws Exception {
int number = 0;
int numberReturn = 1;
try {
numberReturn = 10 / number;
} catch (Exception e) {
numberReturn = 3;
return numberReturn;
} finally {
numberReturn = 4;
System.out.println("finally代码被执行");
return numberReturn;
}
}
}
结果
总结
try{
//业务代码
}catch(Exception e){
logger.error("报错",e)
}finally{
}
1.加了try catch finally之后无论程序是否报错,都会走finally,
程序不报错,try业务代码中有return,finally中也有return,则最后会走finally的return
程序报错,try 业务代码中有return,catch中有return,finally中也有return,则最后会走finally中的return
2.加了try catch之后,
程序报错,try业务代码中有return,catch中有return,则会走catch中的return
程序不报错,try业务代码中有return,catch中有return,则会走try中的return
程序报错,try业务代码中没有return,catch中有return,try catch最后有return,则会走catch中的return
没报错,try业务代码中没有return,catch中有return,try catch最后有return,则会走try catch之后的return
3.try 中有return,catch中有return,finally中有return,程序不报错,最后会返回finally中的return,finally代码会执行
4.try 中有return,catch中有return,finally中有return,程序报错,catch和finally中的代码会执行,最终会走finally中的return
5.try 中有return,catch中有return,finally中无return,程序报错,catch和finally中的代码会执行,最终会在finally执行完之后,一起在catch中返回return
6.try 中有return,catch中有return,finally中无return,程序不报错,finally中的代码会执行,最终会在finally中执行完之后,一起在try中的return
如遇到知识性的错误请留言