首页 > 代码库 > 简单的try-catch异常捕捉
简单的try-catch异常捕捉
package com.test;public class TryCatchTest { public static void main(String[] args) { TryCatchTest tct = new TryCatchTest(); int result = tct.test(); System.out.println("test()方法执行完毕,返回"+result); int result2= tct.test2(); System.out.println("test2()执行完毕"); int result3 = tct.test3(); System.out.println("test3()执行完毕"); } //创建divider(除数) //result结果 //try-catch捕获while循环 public int test(){ int divider = 10; int result = 100; try { while(divider >-1){ divider--; result = result +100/divider; } return result; } catch (Exception e) { e.printStackTrace();//打印错误原因与位置 System.out.println("循环抛出异常"); return -1; } } public int test2(){ int divider = 10; int result = 100; try { while(divider >-1){ divider--; result = result +100/divider; } return result; } catch (Exception e) { e.printStackTrace();//打印错误原因与位置 System.out.println("循环抛出异常"); return result = 999; }finally{ System.out.println("这是finally"); System.out.println("result"+result); } } //如果try-catch语句中无返回,就会调用之外的return public int test3(){ int divider = 10; int result = 100; try { while(divider >-1){ divider--; result = result +100/divider; } } catch (Exception e) { e.printStackTrace();//打印错误原因与位置 System.out.println("循环抛出异常"); }finally{ System.out.println("这是finally"); System.out.println("result"+result); } System.out.println("test3运行完毕"); return -1; }}
简单的try-catch异常捕捉
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。