代码阅读题
第1题:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/** * @author yhm * @create 2020-07-16 11:09 */ public class Test01 { public static int test(int x,int y){ int result = x; try { if (x < 0 || y < 0){ return 0; } result = x + y; return result; }finally { result = x - y; } } public static void main(String[] args) { int test = test(3,5); System.out.println(test); } }
复制代码
1
2
3
4
5
6
7
8
9/* * (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 * (2)如果finally中有return语句,那么try...catch...finally结构 * 一定从finally中的return回去 * (3)如果finally中没有return语句,那么try...catch...finally结构 * 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值 */
第2题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14/** * @author yhm * @create 2020-07-16 11:22 */ public class Test02 { public static void main(String[] args) { try { return; }finally { System.out.println("finally"); } } }
复制代码
1
2
3
4
5/* * finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 */
第3题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/** * @author yhm * @create 2020-07-16 11:23 */ public class Test03 { { System.out.println("a"); } static { System.out.println("b"); } Test03(){ System.out.println("c"); } public static String getOut(){ try { return "1"; }catch (Exception e){ return "2"; }finally { return "3"; } } public static void main(String[] args) { System.out.println(getOut()); } }
复制代码
1
2
3
4
5
6
7
8
9
10/* * (1)main()执行之前,需要先完成所在类的初始化 * (2)类初始化由两部分代码组成:①静态变量的显式赋值代码②静态代码块 * (3)没有创建对象,不会执行实例初始化相关的代码 * (4)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 * (5)如果finally中有return语句,那么try...catch...finally结构 * 一定从finally中的return回去 */
第4题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/** * @author yhm * @create 2020-07-16 11:28 */ public class Test04 { static int i = 0; public static void main(String[] args) { System.out.println(test()); } public static int test(){ try { return ++i; }finally { return ++i; } } }
复制代码
1
2
3
4
5
6
7
8
9/* * (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 * (2)如果finally中有return语句,那么try...catch...finally结构 * 一定从finally中的return回去,但是try和catch中的return语句中返回值的表达式会执行。 * (3)如果finally中没有return语句,那么try...catch...finally结构 * 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值 */
第5题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25import java.io.IOException; /** * @author yhm * @create 2020-07-16 11:30 */ public class Test05 { public static void main(String[] args) { int a = -1; try { if (a > 0){ throw new RuntimeException(""); }else if (a < 0){ throw new IOException(""); }else return; } catch (IOException ioe){ System.out.println("IOException"); }catch (Throwable e){ System.out.println("Throwable"); }finally { System.out.println("finally"); } } }
复制代码
1
2
3
4
5
6
7
8/* * (1)throw用于手动抛出异常 * (2)无论是JVM抛出的异常还是throw抛出的异常都需要catch处理 * (3)catch是多分支结构,从上到下按顺序匹配,只会进入第一个匹配上的catch分支 * (4)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 */
第6题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/** * @author yhm * @create 2020-07-16 11:34 */ public class Test06 { public static int fun(){ int result = 5; try { result = result / 0; return result; }catch (Exception e){ System.out.println("Exception"); result = -1; return result; }finally { result = 10; System.out.println("I am in finally"); } } public static void main(String[] args) { int x = fun(); System.out.println(x); } }
复制代码
1
2
3
4
5
6
7
8
9/* * (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 * (2)如果finally中有return语句,那么try...catch...finally结构 * 一定从finally中的return回去 * (3)如果finally中没有return语句,那么try...catch...finally结构 * 才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值 */
第7题
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23/** * @author yhm * @create 2020-07-16 11:36 */ public class Test07 { public static int aMethod(int i)throws Exception{ try { return i / 10; }catch (Exception ex){ throw new Exception("exception in aMethod"); }finally { System.out.println("finally"); } } public static void main(String[] args) { try { aMethod(0); }catch (Exception e){ System.out.println("exception in main"); } } }
复制代码
1
2
3
4
5
6
7/* * (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常, * 也不管try和catch中是否有return语句,都会执行的部分 * (2)throw用于手动抛出异常 * (3)throws用于将异常抛给调用者处理 */
最后
以上就是俏皮钻石最近收集整理的关于章节练习:异常处理的全部内容,更多相关章节练习内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复