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

外贸网站 在线留言wordpress _x

外贸网站 在线留言,wordpress _x,建设农业网站,5151ppt网站建设1.异常的概念 在程序运行时,打断正常程序流程的不正常情况分两类: 1.错误(Error):应用程序无法捕获的严重问题(自己无法处理) 例: 虚拟机相关的问题,如虚拟机崩溃、动态链接失败、低层资源错误等 总是不受编译器检查的&#xff0…
1.异常的概念

在程序运行时,打断正常程序流程的不正常情况分两类:
1.错误(Error):应用程序无法捕获的严重问题(自己无法处理)
例:
虚拟机相关的问题,如虚拟机崩溃、动态链接失败、低层资源错误等
总是不受编译器检查的(Unchecked)
可以被抛出,但无法恢复,不可能被捕获
2.异常(Exception):应用程序可捕获的一般问题(自己可以处理)
例:
试图打开的文件不存在
网络连接中断
数组越界
要加载的类找不到
……
在这里插入图片描述

异常的分类:

  • Runtime异常(免检异常)–经常出现的异常但没必要花大量精力去处理(不可捕获):由Runtime异常类(继承Exception类)及其子类表示的异常,如数组越界、算术运算异常、空指针异常等。
  • 必检异常–不经常出现但影响力很大(可捕获):除Runtime异常类及其子类之外的所有异常,如文件不存在、无效URL等。
public class TestEx {public static void main(String[] args) {int [] array = {1,2,3};//System.out.println(array[3]);//抛出异常//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3//at java005.TestEx.main(TestEx.java:6)//可以去帮助文档查ArrayIndexOutOfBoundsException//System.out.println(10/0);//抛出异常//Exception in thread "main" java.lang.ArithmeticException: / by zero//at java005.TestEx.main(TestEx.java:11)}
}
2.Java的异常类层次

Java.lang.Throwable是所有异常类的父类
1.检索异常的相关信息
2.输出显示异常发生位置的堆栈追踪轨迹
在这里插入图片描述
ArithmetcException:整数的除0操作导致的异常,如:int i=10/0;
NullPointerException:对象未实例化时,即试图通过该对象的引用访问其成员变量或方法,如Date d=null; System.out.println(d.toString());
IOException:输入/输出时可能产生的各种异常
(所有继承Exception类的子类都以Exception结尾)

3.异常处理方法
  • 捕获并处理异常
  • 将方法中产生的异常抛出
3.1捕获并处理异常

通过try-catch-finally语句来实现,基本格式:
try{ /** 监控区域 /
//一条或多条可能抛出异常的Java语句
}catch(ExceptionType1 e1){ /
* 异常处理程序 /
//捕获到ExceptionType1类型的异常时执行的代码
}catch (ExceptionType2 e2){ /
* 异常处理程序 */
//捕获到ExceptionType2类型的异常时执行的代码
} …
finally{
//执行最终清理的语句
}
try-catch-finally可以实现嵌套

举例:

		try {			System.out.println(10/0);System.out.println("1");System.out.println(array[3]);}catch(ArrayIndexOutOfBoundsException aioobe){System.out.println("2");}catch(ArithmeticException ae) {System.out.println("3");}finally {System.out.println("4");}System.out.println("5");

在这里插入图片描述

		try {System.out.println(array[3]);			System.out.println(10/0);System.out.println("1");}catch(ArrayIndexOutOfBoundsException aioobe){System.out.println("2");}catch(ArithmeticException ae) {System.out.println("3");}finally {System.out.println("4");}System.out.println("5");

在这里插入图片描述

		try {System.out.println(array[3]);			System.out.println(10/0);System.out.println("1");}catch(ArithmeticException ae) {System.out.println("3");}finally {System.out.println("4");}System.out.println("5");

在这里插入图片描述
在这里插入图片描述

注意:在try语句块内,只要遇到第一个异常就会执行catch语句,剩下的不再执行,finally 语句块可以省略,若finally语句块存在,则无论是否发生异常均执行

package java005;public class TestEx {public static void main(String[] args) {TestEx t = new TestEx();
//28行		t.m(0);}public void m(int n) throws ArithmeticException{if(n==0) {
//32行			ArithmeticException e = new ArithmeticException("除数不能为0");throw e;//可理解为传入到了catch(ArithmeticException ae){ae.printStackTrace();//返回堆栈路径ae.getMassage();}}}
}

在这里插入图片描述

3.2多种异常同时处理
FileInputStream in = null;try {in = new FileInputStream("a.txt");int b = in.read();while(b != 1) {b = in.read();System.out.println(b);}}catch (FileNotFoundException fe) {fe.printStackTrace();}catch (IOException ioe) {//IOException是FileNotFoundException的父类,catch按照从上往下的顺序运行,故子类应排在父类之前ioe.printStackTrace();}finally {try {in.close();}catch(IOException e) {e.printStackTrace();}}

封装一下

public void readMethod(String fileName) {FileInputStream in = null;try {in = new FileInputStream(fileName);int b = in.read();while(b != 1) {b = in.read();System.out.println(b);}}catch (FileNotFoundException fe) {fe.printStackTrace();}catch (IOException ioe) {//IOException是FileNotFoundException的父类,catch按照从上往下的顺序运行,故子类应排在父类之前ioe.printStackTrace();}finally {try {in.close();}catch(IOException e) {e.printStackTrace();}}}

如果不想在readMethod方法里面实现处理异常,将该异常抛出到调用该方法的程序

	public void readMethod(String fileName)throws FileNotFoundException,IOException {FileInputStream in = null;in = new FileInputStream(fileName);int b = in.read();while(b != 1) {b = in.read();System.out.println(b);}}public void callRead() {try {readMethod("a.txt");}catch (FileNotFoundException fe) {fe.printStackTrace();}catch (IOException ioe) {//IOException是FileNotFoundException的父类,catch按照从上往下的顺序运行,故子类应排在父类之前ioe.printStackTrace();}}

若一个异常在转向到main()后还未被处理,则程序将非正常终止。
在这里插入图片描述

4.自定义异常类

在这里插入图片描述

public class ServerTimeOutException extends Exception{private String reason;private int port ;public ServerTimeOutException(String reason, int port){this.reason = reason;this.port = port;}public String getReason(){return reason;}public int getPort(){return port;}
}
Public void connectMe(String serverName) throws ServerTimeOutException{int success;int portToConnect = 80;success = open(serverName, portToConnect);if(success= -1){throw new ServerTimedOutException(“Couldnot connect”,80);}
}
 Public void findServer(){…try{connectMe(defaultServer);} catch (ServerTimeOutException e){System.out.println(“Server timed out, try another”);}
}
4.1方法重写抛出异常

注意:重写方法需要抛出与原方法所抛出异常类型一致的异常或者不抛出异常
在这里插入图片描述

http://www.dtcms.com/a/400812.html

相关文章:

  • 网站例子大全wordpress锚点定位
  • wap网站源代码邢台业之峰装饰公司怎么样
  • 坪山网站建设行业现状北京十大品牌装修公司
  • 做泥网站大量增加告权重网站友链回提升网站权重吗
  • 东莞中企动力做网站教做视频的网站
  • 邯郸现代建设集团网站建设云个人网站
  • 网站该怎么做企业解决方案英文
  • 深圳市住房和建设局官方网站查询网站备案照片要求
  • 南宁建站价格中国联通与腾讯设立合作
  • jfinal怎么做网站盐都区城乡建设局网站
  • 软考中级习题与解答——第十一章_法律法规与标准化(2)
  • 实验中心网站建设老板让做公司网站设计
  • 广告推广怎么做青岛seo整站优化公司
  • 电子商务网站开发实存内容网站界面设计需求
  • 网站可以做参考文献吗家居网站建设平台
  • 做电商网站用什么技术制作离婚证的小程序
  • 杭州电商网站开发公司团队建设
  • 有没有专门做网站的做网站找哪家又便宜又好
  • 南和网站建设公司推广普通话喜迎二十大的手抄报怎么画
  • 企业网站建设方案优化做网站思路
  • 怎样用wordpress做网站宜昌市住房和城乡建设局网站
  • 寮步网站建设哪家好网站为什么做微云的采集
  • 网站建设新闻咨询word做招聘网站
  • 苏州最大的网站建设公司北京网站建设销售招聘
  • 沈阳网络推广建站汕头推广公司
  • 做网站多少钱啊学设计的基础是什么
  • 浏阳网站开发闵行工程建设网站
  • 国际婚恋网站排名文化品牌建设
  • 对于网站运营应该如何做郑州建站程序
  • 化妆品公司网站模板织梦网站logo修改