try catch finally总结

try-catch-finally 异常的处理方式之一:捕获异常 捕获异常是通过3个关键词来实现的:try-catch-finally。用try来执行一段程序,如果出现异常,系统抛出一个异常,可以通过它的类型来捕捉(catch)并处理它,最后一步是通过finally语句为异常处理提供一个统一的出口,finally所指定的代码都要被执行(catch语句可有多条;finally语句最多只能有一条,根据自己的需要可有可无)。如图所示。 ... 阅读详情

转载自:https://www.cnblogs.com/superFish2016/p/6687549.html

在网上看到一些异常处理的面试题,试着总结一下,先看下面代码,把这个方法在main中进行调用打印返回结果,看看结果输出什么。

public static  int testBasic(){
        int i = 1; 
        try{
            i++;
            System.out.println("try block, i = "+i);
        }catch(Exception e){
            i ++;
            System.out.println("catch block i = "+i);
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);
        }
        return i;
}

没错,会按照顺序执行,先执行try内代码段,没有异常的话进入finally,最后返回,那么输出如下:

 

try block, i = 2
finally block i = 10
main test i = 10

 

这个没有问题,如果我们把return语句放入try catch里又会怎么样呢

public static  int testBasic(){
        int i = 1; 
        try{
            i++;
            System.out.println("try block, i = "+i);
            return i;
        }catch(Exception e){
            i ++;
            System.out.println("catch block i = "+i);
            return i;
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);
        }
}

输出结果是:

 

try block, i = 2
finally block i = 10
main test i = 2

 

代码顺序执行从try到finally,由于finally是无论如何都会执行的,所以try里的语句并不会直接返回。在try语句的return块中,return返回的引用变量并不是try语句外定义的引用变量i,而是系统重新定义了一个局部引用i’,这个引用指向了引用i对应的值,也就是2,即使在finally语句中把引用i指向了值10,因为return返回的引用已经不是i,而是i',所以引用i的值和try语句中的返回值无关了。

 

但是,这只是一部分,如果把i换成包装类型而不是基本类型呢,来看看输出结果怎样,示例如下:

public static  List<Object> testWrap(){
        List<Object> list = new ArrayList<>();
        try{
            list.add("try");
            System.out.println("try block");
            return list;
        }catch(Exception e){
            list.add("catch");
            System.out.println("catch block");
            return list;
        }finally{
            list.add("finally");
            System.out.println("finally block ");
        }
}

打印结果如下:

try block
finally block 
main test i = [try, finally]

 

可以看到,finally里对list集合的操作生效了,这是为什么呢。我们知道基本类型在栈中存储,而对于非基本类型是存储在堆中的,返回的是堆中的地址,因此内容被改变了。

 

好了,现在我们在finally里加一个return,看看语句是从哪里返回的。

public static  int testBasic(){
        int i = 1; 
        try{
            i++;
            System.out.println("try block, i = "+i);
            return i;
        }catch(Exception e){
            i ++;
            System.out.println("catch block i = "+i);
            return i;
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);
            return i;
        }
}

输出结果如下:

try block, i = 2
finally block i = 10
main test i = 10

可以看到,是从finally语句块中返回的。可见,JVM是忽略了try中的return语句。但IDE中会对finally中加的return有黄色警告提示,这是为什么呢,在try里加入一行会执行异常的代码,如下:

public static  int testBasic(){
        int i = 1; 
        try{
            i++;
            int m = i / 0 ;
            System.out.println("try block, i = "+i);
            return i;
        }catch(Exception e){
            i ++;
            System.out.println("catch block i = "+i);
            return i;
        }finally{
            i = 10;
            System.out.println("finally block i = "+i);
            return i;
        }
}

打印结果如下:

catch block i = 3
finally block i = 10
main test i = 10

可以看到,因为finally中有return语句,try、catch中的异常被消化掉了,屏蔽了异常的发生,这与初期使用try、catch的初衷是相违背的,因此编译器也会提示警告。

那如果在finally中有异常发生,会对try、catch中的异常有什么影响呢?

public static  int testBasic(){
        int i = 1; 
        try{
            i++;
            Integer.parseInt(null);
            System.out.println("try block, i = "+i);
            return i;
        }catch(Exception e){
            String.valueOf(null);
            System.out.println("catch block i = "+i);
            return i;
        }finally{
            i = 10;
            int m = i / 0;
            System.out.println("finally block i = "+i);
        }
}

这里我们在try、catch里强行加上异常语句,打印结果如下:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at tryandcatch.TryAndCatch.testBasic(TryAndCatch.java:25)
at tryandcatch.TryAndCatch.main(TryAndCatch.java:45)

这个提示表示的是finally里的异常信息,也就是说一旦finally里发生异常,try、catch里的异常信息即被消化掉了,也达不到异常信息处理的目的。

 

总结以上测试:

1、finally语句总会执行

2、如果try、catch中有return语句,finally中没有return,那么在finally中修改除包装类型和静态变量、全局变量以外的数据都不会对try、catch中返回的变量有任何的影响(包装类型、静态变量会改变、全局变量)

3、尽量不要在finally中使用return语句,如果使用的话,会忽略try、catch中的返回语句,也会忽略try、catch中的异常,屏蔽了错误的发生

4、finally中避免再次抛出异常,一旦finally中发生异常,代码执行将会抛出finally中的异常信息,try、catch中的异常将被忽略

 

所以在实际项目中,finally常常是用来关闭流或者数据库资源的,并不额外做其他操作。

try、catch、finally 和 return 的执行关系 finally 块的代码总是会执行,无论 try 块是否有异常,是否有 return,还是程序中抛出了异常。即使 try 或 catch 块中有 return,finally 块也会在 return 之前执行。如果 finally 块中包含 return,它会覆盖 try 或 catch 中的 return 结果,直接返回 finally 的返回值。这些规则保证了 finally 块的执行一致性,但也可能引发一些难以察觉的问题,所以在编写代码时需要特别小心 finally 中的逻辑。 阅读详情

相关推荐

Agent 长期记忆三代演进:从 RAG 到 Memory Graph,mem0 / Zep / LangMem / Letta 深度横评

文章摘要 2026年,Agent长期记忆技术迎来关键突破,OpenAI、Anthropic等厂商通过扩大上下文窗口和推出记忆工具,推动长期记忆从工程补丁发展为独立技术层。本文通过分析三代记忆架构演进(RAG→结构化Fact→MemoryGraph)和四大方案(mem0/Zep/LangMem/Letta)的8维度横评,揭示以下核心观点: 长上下文≠长期记忆:128K上下文如同RAM,仍需硬盘式记忆层筛选和结构化存储,避免“Context Rot”导致的准确率下降。 三代架构演进: Gen1(RAG):向量检

LDZKKJ的博客 1429

try、catch、finally的执行顺序

1. 不带return的执行顺序 异常处理中,try、catch、finally的执行顺序: 如果try中没有异常,执行顺序为:try->finally 如果try中有异常,执行顺序为:try->catch->finally public class Test { public static int rank; public static void main(String[] args) { rank = 1; solve2(); System.ou

xingyu19911016的博客 1万+

广工计组f4b 8位12指令微程序CPU设计实验报告(内含可通过代码以及波形图)

实验目的:模型机是计算机的缩细模型,通过它可以理解计算机整机的结构和功能,理解CPU,存储器,中断控制器,接口的结构及实现逻辑和各部件之前的接口关系。本实验是设计一个8位模型计算机。:一条机器指令可以分解成一个微操作序列,这些微操作是计算机最基本的,不可再细分的操作。:在微程序控制的计算机中。将控制部件向执行部件发出的各种控制命令成为微命令,它是构成控制序列的最小单位。微命令和微操作是一一对应的。微命令是微操作的控制信号,微操作微命令的执行过程。2.:微指令是若干微命令的集合。

weixin_63692154的博客 4705

try/catch-finally的执行顺序

题记 try-catch后只要有finally,那么finally就一定会被执行;不管try-catch是否有return,finally仍然会执行,如果finally中有return,则只会返回finally中的return的值。 try/catch-finally的执行顺序可以分为:有return和没有return来讨论 一、无return 1.如果没有捕捉到异常,执行try代码块,而不会执行catch的代码块,最后执行finally代码块。其执行顺序:try-->fin...

HandCream123的博客 6153

Prometheus安装部署说明

1. 安装Prometheus Server   Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖。用户只需要下载对应平台的二进制包,解压并且添加基本的配置即可正常启Prometheus Server。 1.1 下载并解压二进制安装包 通过Prometheus的官方网站:https://prometheus.io/download/,下载最新版本的Prometheus安装包 #下载、解压、创建软链接 cd /export/ wget https://githu..

m0_38100619的博客 5013

ST7102驱动移植与调试实战

本文详细介绍了在君正T40平台上移植和调试ST7102 LCD驱动芯片的实战经验。内容涵盖硬件接口配置、时序参数调整、寄存器初始化序列编写及常见问题解决,重点解析了ST7102单芯片集成显示与触控功能的特性,为嵌入式设备显示驱动开发提供实用指南。

k6l7m8n9的博客 1413

Java在异常处理中try、catch、finally的执行顺序

通过学习Java的异常处理,知道它是通过try-catch-finally来实现的,那么就有一个疑问?他们的执行顺序是如何的,接下来我将tong'g

qq_48241564的博客 6104

新凯来嵌入式面经及参考答案

利用编译器预定义宏判断目标架构,如。

大模型大数据攻城狮的专栏 2065

try catch Finally

程序首先执行try块中的代码,如果有异常则执行catch块中的代码,无论如何都会执行finally块中的代码2。再执行finally块,最后执行try中return;try、catch和finally的执行顺序是按顺序执行的,具体来说,如果try中没有异常,则顺序为try→finally;程序首先执行try块中的代码,如果有异常则执行catch块中的代码,无论如何都会执行finally块中的代码2。在catch块的return语句执行完后,并不直接返回而是执行finally块中的代码,最后返回结果63。

B__B_B的博客 610

2023年GESP12月认证C++五级真题解析

例如,如果a=4,那么4是最⼩的幸运数,而1不是,但我们可以连续对1做3次+1操作,使其变为4,所以我们可以说,1幸运化后的结果是 4。【题目大意】给一个完全平方数的标准,推导出哪些是超级幸运数,然后判断N个数字中,哪些是超级幸运数,是的输出“lucky”,不是的输出离该数字最近的比它大的数字。前一个数字,下标是j的数字是偶数,后面的数字下标是j+1的是奇数,按照要求,偶数在奇数的后面,要交换。A函数时间复杂度是O(n/2),B函数算法是O(sqrt(n)),大部分情况后者是优的,值更小。

lan_in的博客 1007

看GRE协议的数据封装

GRE网络数据封装流程

FanFF的博客 80

try catch finally 执行顺序面试题总结

在网上看到一些异常处理的面试题,试着总结一下,先看下面代码,把这个方法在main中进行调用打印返回结果,看看结果输出什么。 public static int testBasic(){ int i = 1; try{ i++; System.out.println("try bl...

ajaxeej2228的博客 1322

PCL基础:getTransformation函数全流程解析与应用实例,一篇文章精通点云变换

在 Eigen 和 PCL 中,getTransformation是一个常用的函数,用于根据输入的平移和旋转信息(如欧拉角或四元数)生成一个仿射变换矩阵(Eigen::Affine3f或 Eigen::Affine3d)。1)在 Eigen 中,可以通过四元数和平移向量构造一个变换矩阵。2)讲解了从四元数转换到欧拉角,或者从欧拉角转换到四元数的方法。

weixin_42896234的博客 1368

try、catch、finally 用法总结

try catch finally 使用 整体执行流程如下,有图看着更容易理解。 详细说明和代码 try-catch-finally中无return时   如果try块中出现异常则进入catch中,执行完catch中代码后进入finally,如果catch中出现异常仍然会执行finally代码块,finally块执行结束后抛出异常。try块中无异常时执行完try块直接执行finally。 以catch中抛出异常为例,代码如下: 执行结果如下: try-catch-finally中包含return时  

weixin_42168421的博客 3万+

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

try-catch-finally的执行顺序

try-catch-finally的执行顺序 结论: 不管有没有出现异常,finally代码块都会执行; 不管try和catch的代码块中有return时,finally仍会执行,且如果finally代码块也有return,则此代码肯定会返回finally执行的return值。 分有return和没有return来讨论try-catch-finally执行顺序的情况: 1 无return 举个例子: try { t(); tt(); } catch (Exception e) {

qq_43842093的博客 1万+

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Java中try、catch、finally的使用

Java中try、catch、finally的使用

m1992222的博客 9593

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

JavaScript中try{}catch{}finally的使用

try{}catch{}finally{}的使用 try{}catch{}finally{} 语句一般是用来将有可能引发错误的代码(比如异步请求)放在try语句块中,并且对应一个响应,然后有异常被抛出到catch中。 语法: try { //有可能引发错误的代码 } [catch (exception_var_1 if condition_1) { //处理错误为condition_1的情况 ...

吴小迪的博客 4万+

Java 的 try catch finally 执行顺序

Java 的 try catch finally 执行顺序

26857259 2358

try catch finally 使用方法详解

本文是一个老前辈写的java中的try、catch、finally的用法文章,下面的9个例子,把涵盖的所有用法基本都涵盖了,值得大家细细的观看,很有用。看完也就知道到底该怎么用try、catch了。希望对大家有所帮助。文末有我写的一些用法总结。用好try catch很重要,在处理多线程、避免线程阻塞中很有用,同时养成良好的异常处理习惯,也是作为一个程序员的必备素养。 转载自:http://www....

a116385895的博客 5815

try,catch,finally用法

例三:(可看完后面的break知识点,再来看这个例子) 如果try语句后面有finally,try中就算有break用来跳出语句块,也不管用 只要有finally,不管try和catch语句中执行什么,一定会进入finally语句。注意: catch 和 finally 语句都是可选的,但你在使用 try 语句时必须至少使用一个(也就是try必须搭配catch或者finally)。​ 如果try语句后面有finally,try中的return不会跳出函数,因为一定要进入finally语句。

我的博客简介 2013

try 、catch、finally用法总结

1. try 、catch、finally用法总结 1、在进行异常的处理之后,在异常的处理格式中还有一个finally语句,那么此语句将作为异常的统一出口,不管是否产生了异常,最终都要执行此段代码。 2、当try、catch中有return时,finally中的代码依然会继续执行 3、finally是在return后面的表达式运算之后执行的,此时并没有返回运算之后的值,而是把值保存起来,不管finally对该值做任何的改变,返回的值都不会改变,依然返回保存起来的值。也就是说方法的返回

qq_42295268的博客 1万+

try-catch-finally的用法

本篇为在复习过程中发现应该掌握的java基础知识。try-catch用来捕获异常(checked异常)。适用于:在某个类的方法中会抛出异常(这应该在spec中声明好)。

through312的博客 4318

try,catch,finally的用法

try/catch/finally 语句用于处理代码中可能出现的错误信息。 错误可能是语法错误,通常是程序员造成的编码错误或错别字。也可能是拼写错误或语言中缺少的功能(可能由于浏览器差异) try语句允许我们定义在执行时进行错误测试的代码块。 catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块。 finally 语句在 try 和 catch 之后无论有无异常都会执行。 注意...

Lemontree_fu的博客 1960

try catch finally 用法

在讲之前我们先看一段程序: public class Test { public static void main(String[] args) { System.out.println("return value of getValue(): " + getValue()); } public static int getValue() { ...

疯狂的蜗牛 2万+

try,catch,finally的用法

1.使用try,catch,finally语法处理异常可以使代码在异常的情况下继续执行。 2.当catch块指定捕捉的异常与代码异常不同时,程序会被终止。 3.当catch块没有指定捕捉异常内容时,任何异常都会被捕捉。 4.当代码没有异常时,catch块的内容不会被执行。 5.只要程序能够执行,finally块的内容都会被执行。 finally语句块的作用 finally里一...

qq_35324569的博客 1万+

理解js中的try,catch,finally

try/catch/finally 语句用于处理代码中可能出现的错误信息。

oooooorangeeee的博客 2116
上一篇: 关于Android studio的模拟器无法打开问题
下一篇: Android中的MVC和MVP
android_seven
博客等级 码龄11年 8粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值