首页 > 其他分享 >try catch return语句情况分析

try catch return语句情况分析

时间:2024-06-17 11:31:04浏览次数:12  
标签:语句 return res try finally catch 赋值

try catch return语句情况分析

try catch

try catch语法是一种对应于异常处理的语句,其中try语句内用于编写有异常存在可能的语句,而catch语句内用于编写捕获到异常的类型以及对异常对象的处理方法,本文主要以java语言为示例来演示try catch语句关于return情况的分析。
示例代码如下,后续的测试都会基于当前代码改动测试

public class DemoController {


    public static void main(String[] args) {

        String test = test();

        System.out.println(test);

    }


    static String test() {

        String res = null;

        try {

            //int i = 1/0;

            res = "one";

        }catch (Exception e) {

            e.printStackTrace();

            res = "two";

        }finally {

            res = "three";

        }

        return res;

    }

}

无finally语句

下面开始讨论对应的情况:
1.无异常,try语句正常执行,结果返回try语句中的赋值数据,执行结果如下
在这里插入图片描述
2.有异常,执行catch中的赋值语句,打印异常信息,返回catch中赋值数据,执行结果如下
在这里插入图片描述
有finally语句
1.无异常,因为finally中有赋值,所以返回finally中赋值数据,执行结果如下
在这里插入图片描述
2.有异常,打印错误信息,同时因为finally中有赋值,所以返回finally中赋值数据,执行结果如下
在这里插入图片描述

写在最后

由于try catch finally结构中finally中代码一定会执行,因此建议不要在finally中放置返回数据的赋值语句,如果finally中有当前方法返回的最后数据赋值语句的话,那么即使try语句中业务代码执行成功,返回的赋值信息仍然不是业务需要的数据。

标签:语句,return,res,try,finally,catch,赋值
From: https://blog.csdn.net/csdn565973850/article/details/139739149

相关文章

  • try-with-resources语法
    try-with-resources语法try-with-resources是Java7引入的一种语法结构,用于简化资源管理。资源在使用完毕后会自动关闭,避免了手动关闭资源的麻烦和潜在的资源泄露问题。资源是指任何实现了java.lang.AutoCloseable接口的对象,例如文件输入输出流、数据库连接等。importjava......
  • 13.docker registry(私有仓库)
    dockerregistry(私有仓库)1.从公有仓库中下载镜像比较慢,比如dockerrun执行一个命令假设本地不存在的镜像,则会去共有仓库进行下载。2.如果要是2台机器之间进行拷贝,则拷贝的是完整的镜像更消耗空间。3.如果1个机器上传私有仓库,其他机器均可使用私有仓库的镜像,比较快。缺......
  • 14.带basic认证的registry仓库
    带basic认证的registry安装httpd-tools工具,将账号密码重定向到指定路径下的文件yuminstallhttpd-tools-ymkdir/opt/registry-var/auth/-phtpasswd-Bbnquyunlong123456>>/opt/registry-var/auth/htpasswd每次追加密码文件htpasswd,里面的值都是不一样的##......
  • ESP-IDF OTA升级过程中遇到的“esp_transport_read returned:-1 and errno:128”问题(4
    接前一篇文章:ESP-IDFOTA升级过程中遇到的“esp_transport_readreturned:-1anderrno:128”问题(3)上一回讲到,笔者准备第二天围绕信号强度展开进一步测试。实际上没等到第二天,笔者在当天下午下班时间(18点)以后就进行了相关测试(不过测试倒并不是完全针对于信号强度的)。笔者运......
  • WPF Path GeometryCombineMode Union, Exclude,Intersect,Xor
    union<Windowx:Class="WpfApp172.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF StreamGeometry
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usin......
  • WPF Path Data PathGeometry PathFigure Segments BezierSegment,LineSegment,ArcSeg
     BezierSegment//BezierCurveusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documen......
  • fegin的retry机制
    服务间调用如果因为网络原因访问失败了,也可以考虑使用fegin的重发功能来重新访问服务。配置了ribbon或者loadBalance后,重发会根据负载均衡规则寻找新服务。举例:feignServer客户端访问userModel服务(两个节点userModel1,userModel2)如果使用轮询策略(负载均衡)且就爱社userModel1网......
  • WPF Image Image clip EllipseGeometry
    <Windowx:Class="WpfApp169.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Path LineGeometry,RectangleGeometry,EllipseGeometry
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows......