当前位置:网站首页>看一看try{}catch{}
看一看try{}catch{}
2022-07-17 19:44:00 【暗物质鬼】
摸鱼第三天,今天问了带我的导师今天做什么,他说接着看文档,已经三天啦。坐不住了,我想直接拉代码理清楚业务。可是公司分配的内外ip一直不下来,打开牛客网刷一刷题。
遇到一个很有意思的题
给定以下代码,输出的内容是()
public class Test {
public static void main(String[] args) {
try {
test();
System.out.println("A");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("B");
return;
} catch (Exception e) {
System.out.println("C");
} finally {
System.out.println("D");
}
}
private static void test() {
int[] a = {
1, 2, 4};
System.out.println(a[3]);
}
}

我一开始也是有点懵,我的解题思路。
test()报错被捕获,所以A不会打印排除AD答案,在try{}catch(){}finally{}语句中,不论怎么样finally的代码块一定会执行,包括在catch语句里面包含return语句,在return之前会执行finally。所以选择B,
可是我纠结的是到底执不执行C,其实如果答案有BCD的话我会选这个的喔,运气好选对了。
百度一下:在try{}catch(){}catch(){}finally{}语句中,多个catch中代码是顺序执行,如果匹配到最合适的异常就会执行该代码块并且不会往下执行,所以打印结果只能是BD,
每日一问,如果finally中也有return,那么最后函数会返回catch中的return还是finally中的呢?评论区告诉我
边栏推荐
- uniapp 高德地图定位功能
- Go-Excelize API源码阅读(三)——OpenReader()
- NO.6浮点数的表示与运算
- Uniapp Gaode map positioning function
- ModuleNotFoundError: No module named ‘_ distutils_ hack‘
- How to upgrade Flink job gracefully?
- Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL
- [code hoof set novice village question 600] formatted input and output, using 0 to replace the completed space
- Onvif protocol related: 4.1.1 WS username token method to obtain wsusernametokenbean
- FreeRTOS-空闲任务和阻塞延时的实现
猜你喜欢

面试记录

Qt之使用QLisView实现QQ登录历史列表

onvif协议相关:4.1.1 WS-Username token方式获取WSUsernameTokenBean

Use of Google browser developer tools (Master!)

onvif协议相关:2.1.2 none方式获取截图url

Onvif protocol related: 4.1.1 WS username token method to obtain wsusernametokenbean

【考研词汇训练营】Day 6 —— eventually,state,create,productivity,stimulate

onvif协议相关:3.1.2 Digest方式获取token列表

Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL

Some common operation commands of the command line and solutions to common errors
随机推荐
Some common operation commands of the command line and solutions to common errors
新建一个eureka client
onvif協議相關:4.1.3 WS-Username token方式獲取截圖url
深度学习从入门到放弃100天挑战
Onvif protocol related: 2.1.2 get screenshot URL in none mode
【码蹄集新手村 600 题】计算一个整数有多少位数
Tke mounts CFS across cloud networking
STL string replication comparison
The cloud audit service CTS is a paid service. The paid items include the opening of tracker, event tracking, storage and retrieval of events within 7 days and other related fees
[code hoof set novice village question 600] align left and right when outputting
【码蹄集新手村 600 题】格式化的输入输出,使用 0 来代替补全的空格
How to earn money in low-level cities
[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
【7.13】代码源 -【饿饿 饭饭】【路径计数2】【函数求和】
Interview records
分析并HOOK SSHD来劫持密码
Acwing game 60
Onvif protocol related: 3.1.3 get screenshot URL in digest mode
Li Kou's 302 weekly match
[code hoof set novice village 600 question] calculate the number of digits of an integer