try-catch-finally 语句是 Java 中常用的异常处理机制,其中 finally 块用于在 try 块和 catch 块执行完毕后执行一些必要的清理操作,例如释放资源、关闭文件等。在使用 try-catch-finally 语句时,需要注意以下几点:
总之,try-catch-finally 语句是 Java 中常用的异常处理机制,使用时需要注意以上几点,以确保程序的正确性和稳定性。
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyException
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyException {
- public static void main(String[] args) {
- log.info("getFinallyException start");
- FinallyService.getFinallyException();
- log.info("getFinallyException end");
- }
- }
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyService
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyService {
- /**
- * finally 块中的代码抛出异常
- *
- * @methodName: getFinallyException
- * @return: void
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static void getFinallyException() {
- try {
- log.info("try");
- int i = 1 / 0;
- log.info("i=" + i);
- } catch (Exception e) {
- log.error("error:", e);
- } finally {
- throw new RuntimeException("finally");
- }
- }
- }
打印日志
- [INFO ] 2023-11-16 16:04:19.549 [main] c.y.j.d.exception.FinallyException - getFinallyException start
- [INFO ] 2023-11-16 16:04:19.552 [main] c.y.j.demo.exception.FinallyService - try
- [ERROR] 2023-11-16 16:04:19.554 [main] c.y.j.demo.exception.FinallyService - error:
- java.lang.ArithmeticException: / by zero
- at com.ybw.jdk8.demo.exception.FinallyService.getFinallyException(FinallyService.java:24)
- at com.ybw.jdk8.demo.exception.FinallyException.main(FinallyException.java:15)
- Exception in thread "main" java.lang.RuntimeException: finally
- at com.ybw.jdk8.demo.exception.FinallyService.getFinallyException(FinallyService.java:29)
- at com.ybw.jdk8.demo.exception.FinallyException.main(FinallyException.java:15)
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyReturn
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyReturn {
- public static void main(String[] args) {
- boolean flag = FinallyService.getFinallyReturn();
- log.info("flag:{}", flag);
- }
- }
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyService
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyService {
- /**
- * finally 块中的代码抛出异常
- *
- * @methodName: getFinallyException
- * @return: void
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static void getFinallyException() {
- try {
- log.info("try");
- int i = 1 / 0;
- log.info("i=" + i);
- } catch (Exception e) {
- log.error("error:", e);
- } finally {
- throw new RuntimeException("finally");
- }
- }
-
-
- /**
- * @methodName: getFinallyReturn
- * @return: boolean
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static boolean getFinallyReturn() {
- int i = 1;
- try {
- int j = i / 0;
- log.info("j:{}", j);
- } catch (Exception e) {
- log.error("捕获异常:", e);
- return false;
- } finally {
- log.info("finally");
- return true;
- }
- }
- }
打印日志
- [ERROR] 2023-11-16 16:13:13.430 [main] c.y.j.demo.exception.FinallyService - 捕获异常:
- java.lang.ArithmeticException: / by zero
- at com.ybw.jdk8.demo.exception.FinallyService.getFinallyReturn(FinallyService.java:43)
- at com.ybw.jdk8.demo.exception.FinallyReturn.main(FinallyReturn.java:14)
- [INFO ] 2023-11-16 16:13:13.432 [main] c.y.j.demo.exception.FinallyService - finally
- [INFO ] 2023-11-16 16:13:13.433 [main] c.y.j.demo.exception.FinallyReturn - flag:true
结果为 finally返回的true。
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyReturn
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyReturn {
- public static void main(String[] args) {
- boolean flag = FinallyService.getFinallyReturn2();
- log.info("flag:{}", flag);
-
- }
- }
- package com.ybw.jdk8.demo.exception;
-
- import lombok.extern.slf4j.Slf4j;
-
- /**
- * @author weixiansheng
- * @version V1.0
- * @className FinallyService
- * @date 2023/11/16
- **/
- @Slf4j
- public class FinallyService {
- /**
- * finally 块中的代码抛出异常
- *
- * @methodName: getFinallyException
- * @return: void
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static void getFinallyException() {
- try {
- log.info("try");
- int i = 1 / 0;
- log.info("i=" + i);
- } catch (Exception e) {
- log.error("error:", e);
- } finally {
- throw new RuntimeException("finally");
- }
- }
-
-
- /**
- * @methodName: getFinallyReturn
- * @return: boolean
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static boolean getFinallyReturn() {
- int i = 1;
- try {
- int j = i / 0;
- log.info("j:{}", j);
- } catch (Exception e) {
- log.error("捕获异常:", e);
- return false;
- } finally {
- log.info("finally");
- return true;
- }
- }
-
- /**
- * @methodName: getFinallyReturn2
- * @return: boolean
- * @author: weixiansheng
- * @date: 2023/11/16
- **/
- public static boolean getFinallyReturn2() {
- int i = 1;
- try {
- int j = i / 0;
- log.info("j:{}", j);
- } catch (Exception e) {
- log.error("捕获异常:", e);
- throw new RuntimeException("catch exception");
- } finally {
- log.info("finally");
- return true;
- }
- }
- }
打印日志
- [ERROR] 2023-11-16 16:20:58.470 [main] c.y.j.demo.exception.FinallyService - 捕获异常:
- java.lang.ArithmeticException: / by zero
- at com.ybw.jdk8.demo.exception.FinallyService.getFinallyReturn2(FinallyService.java:63)
- at com.ybw.jdk8.demo.exception.FinallyReturn.main(FinallyReturn.java:14)
- [INFO ] 2023-11-16 16:20:58.472 [main] c.y.j.demo.exception.FinallyService - finally
- [INFO ] 2023-11-16 16:20:58.472 [main] c.y.j.demo.exception.FinallyReturn - flag:true
catch中的异常没有做处理,依然返回了true。