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

vs做网站需要的插件百度竞价客服电话

vs做网站需要的插件,百度竞价客服电话,wordpress 页面 文章,新生活cms系统下载一.异常的概念 在Java中,异常(Exception)是指程序执行过程中可能出现的不正常情况或错误。它是一个事件,它会干扰程序的正常执行流程,并可能导致程序出现错误或崩溃。 异常在Java中是以对象的形式表示的,…

一.异常的概念

在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/2=3*/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+"/"+n+"="+r);}public static int div(int m, int n) {int r = m / n;return r;}
}

没有进行异常处理,编译运行结果: 程序退出

root@ubuntu:/home/topeet/guyilian# javac Div.java 
root@ubuntu:/home/topeet/guyilian# java Div 6 3
Begin of div
End of div
6/3=2
root@ubuntu:/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)
root@ubuntu:/home/topeet/guyilian# 

三.异常处理

使用try-catch语句进行异常处理:

/* java Div 6 2* 6/2=3*/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+"/"+n+"="+r);}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;}
}
root@ubuntu:/home/topeet/guyilian# javac Div2.java 
root@ubuntu:/home/topeet/guyilian# java Div2 6 3
Begin of div
this is finally of div
End of div
6/3=2
root@ubuntu:/home/topeet/guyilian# java Div2 6 0
Begin of div
java.lang.ArithmeticException: / by zero
this is finally of div
End of div
6/0=0

使用抛出(throw)处理异常:

/* java Div 6 2* 6/2=3*/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+"/"+n+"="+r);}public static int div(int m, int n) throws ArithmeticException {int r = 0;r = m / n;return r;}
}

运行结果: 

root@ubuntu:/home/topeet/guyilian# javac Div4.java 
root@ubuntu:/home/topeet/guyilian# java Div4 6 3
Begin of div
End of div
6/3=2
root@ubuntu:/home/topeet/guyilian# java Div4 6 0
Begin of div
java.lang.ArithmeticException: / by zero
End of div
6/0=0

可以有多个catch语句捕获不同的异常:

/* java Div 6 2* 6/2=3*/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+"/"+n+"="+r);}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;}
}

运行: 

root@ubuntu:/home/topeet/guyilian# javac Div7.java 
root@ubuntu:/home/topeet/guyilian# java Div7 6 2
Begin of div
End of div
6/2=3
root@ubuntu:/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/0=0

http://www.khdw.cn/news/68306.html

相关文章:

  • 网站公司建设公司百度广告开户流程
  • 做网站关键词加到什么位置网络营销到底是干嘛的
  • 官方网站怎么建设的优化大师怎么提交作业
  • 做网站界面的软件滕州百度推广
  • 深圳有哪些网站开发公司长沙百度百科
  • 广州网站优化网站建设seo内部优化方案
  • 商城网站开发的完整流程深圳百度推广关键词推广
  • 高端品牌网站建设有哪些seo网络推广经理
  • 网站视频点播怎么做sem是什么意思中文
  • 做一个个人网页多少钱百度自动优化
  • 建设网站和网页有啥区别软件网站排行榜
  • 利用小程序反向做网站经典网络营销案例
  • ppt做长图网站拼多多怎么查商品排名
  • 国家精品资源共享课程建设网站站长工具域名
  • 网易企业邮箱手机端登录不上咋办一键优化软件
  • .net网站费用天津优化代理
  • 个体工商户能网站备案吗广告文案
  • 专业酒店设计网站建设静态网页设计与制作
  • 做销售的网站广州网站营销seo费用
  • 服务器和域名如何做网站seo网站关键词优化
  • 两学一做党员夜校播放网站seo日常工作都做什么的
  • 在线制作论坛网站代运营公司怎么找客户
  • b2b网站快速做百度权重网站推广的方法有哪些?
  • 做美女网站赚钱吗软文写作发布
  • 深圳设计网站招聘seo零基础视频教程
  • 网站建设 维护购销合同百度人工客服电话
  • seo外贸 网站公司推荐上海百度seo公司
  • 有做网站的吗网店seo
  • 在美国做垂直网站有哪些seo全国最好的公司
  • 网站备案注销 万网网站seo公司哪家好