当前位置: 首页 > news >正文

嘉兴网站建设托管物流网站建设公司

嘉兴网站建设托管,物流网站建设公司,百度网站联盟推广,标智客logo在线设计生成器一.异常的概念 在Java中#xff0c;异常#xff08;Exception#xff09;是指程序执行过程中可能出现的不正常情况或错误。它是一个事件#xff0c;它会干扰程序的正常执行流程#xff0c;并可能导致程序出现错误或崩溃。 异常在Java中是以对象的形式表示的#xff0c;…一.异常的概念 在Java中异常Exception是指程序执行过程中可能出现的不正常情况或错误。它是一个事件它会干扰程序的正常执行流程并可能导致程序出现错误或崩溃。 异常在Java中是以对象的形式表示的这些对象是从java.lang.Throwable类或其子类派生而来。Throwable是异常类层次结构的根类它有两个主要的子类java.lang.Exception和java.lang.Error。 Exception异常java.lang.Exception是表示可检查异常的基类。可检查异常是指在编译时需要显式处理的异常。Exception类及其子类用于表示程序运行过程中可能出现的外部条件、错误或其他可恢复的情况。例如文件未找到、网络连接中断、输入格式错误等。开发人员需要通过捕获或声明这些异常来确保在程序中进行适当的异常处理。 Error错误java.lang.Error是表示严重问题或系统级错误的基类。错误是指那些程序通常无法处理或恢复的情况例如内存溢出、堆栈溢出、虚拟机错误等。与异常不同错误不需要在程序中显式处理因为它们通常表示了无法解决的问题。 异常在Java中通过抛出throw和捕获catch的方式进行处理。当程序执行到可能引发异常的代码时可以使用throw语句手动抛出异常对象。然后可以使用try-catch语句块来捕获异常并在catch块中提供相应的异常处理逻辑。在catch块中可以根据异常的类型执行适当的操作如日志记录、错误报告或异常处理。如果异常没有在当前方法中被捕获处理它将继续向上级调用栈传播直到找到合适的异常处理代码或导致程序终止。 二.未进行异常处理 未进行异常处理的程序 /* java Div 6 2* 6/23*/public class Div {public static void main(String args[]) {int m Integer.parseInt(args[0]);int n Integer.parseInt(args[1]);System.out.println(Begin of div);int r div(m, n);System.out.println(End of div);System.out.println(m/nr);}public static int div(int m, int n) {int r m / n;return r;} } 没有进行异常处理编译运行结果 程序退出 rootubuntu:/home/topeet/guyilian# javac Div.java rootubuntu:/home/topeet/guyilian# java Div 6 3 Begin of div End of div 6/32 rootubuntu:/home/topeet/guyilian# java Div 6 0 Begin of div Exception in thread main java.lang.ArithmeticException: / by zeroat Div.div(Div.java:22)at Div.main(Div.java:14) rootubuntu:/home/topeet/guyilian# 三.异常处理 使用try-catch语句进行异常处理 /* java Div 6 2* 6/23*/public class Div2 {public static void main(String args[]) {int m Integer.parseInt(args[0]);int n Integer.parseInt(args[1]);System.out.println(Begin of div);int r div(m, n);System.out.println(End of div);System.out.println(m/nr);}public static int div(int m, int n) {int r 0;try {r m / n;} catch (ArithmeticException e) {System.out.println(e);} finally {System.out.println(this is finally of div);}return r;} } rootubuntu:/home/topeet/guyilian# javac Div2.java rootubuntu:/home/topeet/guyilian# java Div2 6 3 Begin of div this is finally of div End of div 6/32 rootubuntu:/home/topeet/guyilian# java Div2 6 0 Begin of div java.lang.ArithmeticException: / by zero this is finally of div End of div 6/00使用抛出throw处理异常 /* java Div 6 2* 6/23*/public class Div4 {public static void main(String args[]) {int m Integer.parseInt(args[0]);int n Integer.parseInt(args[1]);int r 0;System.out.println(Begin of div);try {r div(m, n);} catch (ArithmeticException e) {System.out.println(e);}System.out.println(End of div);System.out.println(m/nr);}public static int div(int m, int n) throws ArithmeticException {int r 0;r m / n;return r;} } 运行结果  rootubuntu:/home/topeet/guyilian# javac Div4.java rootubuntu:/home/topeet/guyilian# java Div4 6 3 Begin of div End of div 6/32 rootubuntu:/home/topeet/guyilian# java Div4 6 0 Begin of div java.lang.ArithmeticException: / by zero End of div 6/00可以有多个catch语句捕获不同的异常 /* java Div 6 2* 6/23*/public class Div7 {public static void main(String args[]) {int m 0;int n 0;int r 0;System.out.println(Begin of div);try {m Integer.parseInt(args[0]);n Integer.parseInt(args[1]);r div(m, n);} catch (ArithmeticException e) {System.out.println(main :e);} catch (NumberFormatException e) {System.out.println(main :e);} catch (RuntimeException e) {System.out.println(main :e);}System.out.println(End of div);System.out.println(m/nr);}public static int div(int m, int n) throws ArithmeticException {int r 0;try {r m / n;} catch (ArithmeticException e) {System.out.println(div :e);throw e;}return r;} } 运行  rootubuntu:/home/topeet/guyilian# javac Div7.java rootubuntu:/home/topeet/guyilian# java Div7 6 2 Begin of div End of div 6/23 rootubuntu:/home/topeet/guyilian# java Div7 6 0 Begin of div div :java.lang.ArithmeticException: / by zero main :java.lang.ArithmeticException: / by zero End of div 6/00
http://www.hkea.cn/news/14314941/

相关文章:

  • 企业网站建设的收获成都网站制作scgckj
  • 怎样只做自己的网站自己电脑做电影网站
  • 榆林网站制作公司网站的备案号是如何链接的
  • 北京网站设计公司兴田德润简介wordpress文章首页不显示怎么办
  • 人人商城网站开发成都网站建站推广
  • 深圳三站合一网站建设互联网推广属于什么经营范围
  • 百度手机网站制作信息流广告投放
  • 做企业网站市场分析vs2015做网站
  • 在哪注册网站东风地区网站建设价格
  • 河南省建设监理网站重庆最新网站备案
  • 国际物流网站建设男人互做网站
  • 阿里巴巴网站图片怎么做的优秀的品牌策划案例
  • 做的新网站做百度推广怎么弄做网站要有哪些知识
  • 廊坊网站关键词推广wordpress的文件权限设置
  • 哪里 教做网站带维护做php网站开发能赚钱吗
  • dede网站地图栏目如何上传文件代刷网站建设
  • 深圳建设网站哪家最好南宁3及分销网站制作
  • 宁波信誉好品牌网站设计地址手机怎么管理wifi踢人
  • 昆山兼职做网站wordpress例行维护
  • 青岛seo网站建设济南做网站最好的单位
  • 网站建设招标采购需求paypal网站做外贸
  • 个人备案的网站名称订阅号上链接的网站怎么做的
  • 类似淘宝的网站怎么做宣城市建设银行网站
  • 河南省建设协会网站做企业网站的第一步需要啥
  • 织梦网站专题模板下载几百块钱可以做网站吗
  • 兰州网站移动端优化现在做推广的新渠道有哪些
  • 网站营销优化方案上海市场营销公司
  • WordPress开网站很慢新闻排版设计用什么软件
  • 南宁武鸣区建设局网站微信商城软件开发
  • 怎么做html5网站吗手机端百度收录入口