• try/catch/finally的各种情况


    众所周知,try语句报错,会执行catch语句,然后执行finally,以下这几种情况,看看会如何输出。

    1、try语句中包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "111";
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally正常输出

     2、try语句报错,catch语句抛出错误,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally执行成功,再抛出catch中的错误

     3、try语句报错,catch包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. System.out.println(123);
    13. }
    14. }

    结果:catch中的语句输出成功,finally中的语句输出成功

     4、try和finally中的语句都包含输出

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "123";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     5、try报错,catch和finally包含return语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     6、try包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "789";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. throw new NullPointerException();
    13. }
    14. }

    结果:直接报错

     7、try包含报错,catch包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new RuntimeException();
    8. } catch (Exception e) {
    9. return "456";
    10. } finally {
    11. throw new NullPointerException();
    12. }
    13. }

    结果:直接报错

    通过上面的例子,我们可以得到以下结论:

    1. finally语句存在的话,一定会执行
    2. 若try语句报错,会执行catch语句,如果catch语句也报错,则会先执行finally语句,再抛出catch的错误
    3. try语句或者catch语句return了,finally仍然会执行
    4. 若finally语句中包含return,则会覆盖try或者catch中的return
    5. 若finally语句报错,这个错误一定会抛出,终止程序
  • 相关阅读:
    Mac 快捷键整理
    C语言单链表的算法之插入节点
    Java-方法的详解( 看完之后,我不允许还有人不懂!)
    收藏|机械工程师面试常问问题
    秋招失利,拿到这份“Java 高分指南(25 专题)”,金三银四翻盘有望
    istio安装文档
    四种强制类型转换
    Java并发编程系列34:CountDownLatch使用
    【C++面向对象程序设计】类与对象的引入、this指针
    DAY57 739. 每日温度 + 496.下一个更大元素 I
  • 原文地址:https://blog.csdn.net/qq_49721447/article/details/127983337