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

万网服务器网站建设聊城网站建设项目

万网服务器网站建设,聊城网站建设项目,wordpress只换域名,网站建设话术宝典一、异常的概念与体系结构 1.1异常的概念 在Java中#xff0c;将程序执行过程中发生的不正常行为称为异常。比如之前写代码时经常遇到的#xff1a; 1.算术异常 System.out.println(10 / 0); // 执行结果 Exception in thread main java.lang.ArithmeticExcep…一、异常的概念与体系结构 1.1异常的概念 在Java中将程序执行过程中发生的不正常行为称为异常。比如之前写代码时经常遇到的 1.算术异常 System.out.println(10 / 0); // 执行结果 Exception in thread main java.lang.ArithmeticException: / by zero 2.数组越界异常 int[] arr {1, 2, 3}; System.out.println(arr[100]); // 执行结果 Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 100 3.空指针异常 int[] arr null; System.out.println(arr.length); // 执行结果 Exception in thread main java.lang.NullPointerException 从上述过程中可以看到java中不同类型的异常都有与其对应的类来进行描述。 1.2异常的体系结构 异常种类繁多为了对不同异常或者错误进行很好的分类管理Java内部维护了一个异常的体系结构 从上图中可以看到 1. Throwable是异常体系的顶层类其派生出两个重要的子类, Error 和 Exception 2. Error指的是Java虚拟机无法解决的严重问题比如JVM的内部错误、资源耗尽等典型代表StackOverflowError和OutOfMemoryError一旦发生回力乏术。 3. Exception异常产生后程序员可以通过代码进行处理使程序继续执行。比如感冒、发烧。我们平时所说的异常就是Exception。 1.3异常的分类 1.3.1编译时异常 在程序编译期间发生的异常称为编译时异常也称为受检查异常(Checked Exception) public class Person {private String name;private String gender;int age;// 想要让该类支持深拷贝覆写Object类的clone方法即可Overridepublic Person clone() {return (Person)super.clone();}//编译时报错//Error:(17, 35) java: 未报告的异常错误java.lang.CloneNotSupportedException; 必须对其进行捕获或声明以便抛出 } 1.3.2运行时异常 在程序执行期间发生的异常称为运行时异常也称为非受检查异常(Unchecked Exception) RunTimeException以及其子类对应的异常都称为运行时异常。比如NullPointerException、 ArrayIndexOutOfBoundsException、ArithmeticException。 注意编译时出现的语法性错误不能称之为异常。例如将 System.out.println 拼写错了, 写成了 system.out.println. 此时编译过程中就会出错, 这是 编译期 出错。而运行时指的是程序已经编译通过得到class 文件了, 再由 JVM 执行过程中出现的错误 小知识 1.程序异常的情况 程序正常返回0 程序异常返回1  2.继承图   二、异常的处理 2.1防御式编程 错误在代码中是客观存在的. 因此我们要让程序出现问题的时候及时通知程序猿. 主要的方式: 2.1.1事前防御型 LBYL: Look Before You Leap. 在操作之前就做充分的检查. boolean ret false;ret 登陆游戏(); if (!ret) {处理登陆游戏错误;return;} ret 开始匹配(); if (!ret) {处理匹配错误;return;} ret 游戏确认(); if (!ret) {处理游戏确认错误;return;} ret 选择英雄(); if (!ret) {处理选择英雄错误;return;} ret 载入游戏画面(); if (!ret) {处理载入游戏错误;return;} ...... 缺陷正常流程和错误处理流程代码混在一起, 代码整体显的比较混乱 2.1.2事后认错型 Its Easier to Ask Forgiveness than Permission. 事后获取原谅比事前获取许可更容易. 也就是先操作, 遇到问题再处理. try {登陆游戏();开始匹配();游戏确认();选择英雄();载入游戏画面(); ...} catch (登陆游戏异常) {处理登陆游戏异常;} catch (开始匹配异常) {处理开始匹配异常;} catch (游戏确认异常) {处理游戏确认异常;} catch (选择英雄异常) {处理选择英雄异常;} catch (载入游戏画面异常) {处理载入游戏画面异常;} ...... 优势正常流程和错误流程是分离开的, 程序员更关注正常流程代码更清晰容易理解代码 异常处理的核心思想就是 EAFP。 在Java中异常处理主要的5个关键字throw、try、catch、final、throws。 2.2异常的抛出 在编写程序时如果程序中出现错误此时就需要将错误的信息告知给调用者比如参数检测。在Java中可以借助throw关键字抛出一个指定的异常对象将错误信息告知给调用者。具体语法如下 throw new XXXException(异常产生的原因); 【需求】实现一个获取数组中任意位置元素的方法   public static int getElement(int[] array, int index){if(null array){throw new NullPointerException(传递的数组为null);} if(index 0 || index array.length){throw new ArrayIndexOutOfBoundsException(传递的数组下标越界);} return array[index];}public static void main(String[] args) {int[] array {1,2,3};getElement(array, 3);} 【注意事项】 1. throw必须写在方法体内部 2. 抛出的对象必须是Exception 或者 Exception 的子类对象 3. 如果抛出的是 RunTimeException 或者 RunTimeException 的子类则可以不用处理直接交给JVM来处理 4. 如果抛出的是编译时异常/受查异常用户必须自己处理否则无法通过编译 5. 异常一旦抛出其后的代码就不会执行 2.3异常的捕获 异常的捕获也就是异常的具体处理方式主要有两种异常声明throws 以及 try-catch捕获处理 2.3.1异常声明throws 处在方法声明时参数列表之后当方法中抛出编译时异常用户不想处理该异常此时就可以借助throws将异常抛给方法的调用者来处理。即当前方法不处理异常提醒方法的调用者处理异常。 语法格式修饰符 返回值类型 方法名(参数列表) throws 异常类型1异常类型2...{ } //克隆方法 class People implements Cloneable{protected Object clone() throws CloneNotSupportedException{return super.clone();}} public class Demo2 {public static void main(String[] args) throws CloneNotSupportedException{People people1new People();People people2(People)people1.clone();} } 【注意事项】 1. throws必须跟在方法的参数列表之后 2. 声明的异常必须是 Exception 或者 Exception 的子类 3. 方法内部如果抛出了多个异常throws之后必须跟多个异常类型之间用逗号隔开如果抛出多个异常类型具有父子关系直接声明父类即可。 4.调用声明抛出异常的方法时调用者必须对该异常进行处理或者继续使用throws抛出 public static void main(String[] args) throws IOException {Config config new Config();config.openConfig(config.ini); } 将光标放在抛出异常方法上alt Insert 快速 处理 2.3.2try-catch捕获并处理 throws对异常并没有真正处理而是将异常报告给抛出异常方法的调用者由调用者处理。如果真正要对异常进行处理就需要try-catch。   语法格式 try{// 将可能出现异常的代码放在这里 }catch(要捕获的异常类型 e){// 如果try中的代码抛出异常了此处catch捕获时异常类型与try中抛出的异常类型一致时或者是try中抛出异常的基类时就会被捕获到// 对异常就可以正常处理处理完成后跳出try-catch结构继续执行后序代码 }[catch(异常类型 e){// 对异常进行处理 }finally{// 此处代码一定会被执行到 }]// 后序代码// 当异常被捕获到时异常就被处理了这里的后序代码一定会执行// 如果捕获了由于捕获时类型不对那就没有捕获到这里的代码就不会被执行 注意 1. []中表示可选项可以添加也可以不用添加 2. try中的代码可能会抛出异常也可能不会 3.如果是非受查异常我们并没有对该异常进行处理那么这个异常会交给JVM处理此时程序会异常终止。此时通过try-catch可以解决后面的代码也被执行。 4.try中的语句如果抛出异常那么接下来的在try的异常语句后面的语句不会被执行。 例子一 //try-catch public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();try{System.out.println(num1/num2);}catch(ArithmeticException e){System.out.println(存在输入异常在74行);}} 例子二  public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();try{System.out.println(num1/num2);System.out.println(num1num2);}catch(ArithmeticException e){System.out.println(存在输入异常在74行);}} 例子三假如存在多个异常都会被捕获吗  public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();int[] arraynull;try{System.out.println(num1/num2);System.out.println(array.length);//空指针异常}catch(ArithmeticException e){System.out.println(存在输入异常在74行);}} 也就是说不可能同时捕获多个异常  例子四优化异常代码--更高的可读性 public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();int[] arraynull;try{System.out.println(num1/num2);System.out.println(array.length);//空指针异常}catch(ArithmeticException e){e.printStackTrace();System.out.println(存在输入异常);}catch(NullPointerException e){e.printStackTrace();System.out.println(空指针异常);}} 关于异常的处理方式异常的种类有很多, 我们要根据不同的业务场景来决定. 对于比较严重的问题(例如和算钱相关的场景), 应该让程序直接崩溃, 防止造成更严重的后果对于不太严重的问题(大多数场景), 可以记录错误日志, 并通过监控报警程序及时通知程序猿对于可能会恢复的问题(和网络相关的场景), 可以尝试进行重试.在我们当前的代码中采取的是经过简化的第二种方式. 我们记录的错误日志是出现异常的方法调用信息, 能很快速的让我们找到出现异常的位置. 以后在实际工作中我们会采取更完备的方式来记录异常信息.【注意事项】 1. try块内抛出异常位置之后的代码将不会被执行 2. 如果抛出异常类型与catch时异常类型不匹配即异常不会被成功捕获也就不会被处理继续往外抛直到JVM收到后中断程序----异常是按照类型来捕获的 public static void main(String[] args) {int[] array {1,2,3};getElement(array, 3);}public static void main(String[] args) {try {int[] array {1,2,3};System.out.println(array[3]); // 此处会抛出数组越界异常}catch (NullPointerException e){ // 捕获时候捕获的是空指针异常--真正的异常无法被捕获到e.printStackTrace();} System.out.println(后序代码);} Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 3at day20210917.ArrayOperator.main(ArrayOperator.java:24) 3. try中可能会抛出多个不同的异常对象则必须用多个catch来捕获----即多种异常多次捕获。 public static void main(String[] args) {int[] arr {1, 2, 3};try {System.out.println(before);// arr null;System.out.println(arr[100]);System.out.println(after);} catch (ArrayIndexOutOfBoundsException e) {System.out.println(这是个数组下标越界异常);e.printStackTrace();} catch (NullPointerException e) {System.out.println(这是个空指针异常);e.printStackTrace();} System.out.println(after try catch);}   4.如果多个异常的处理方式是完全相同, 也可以写成这样不建议这样写可读性差   catch (ArrayIndexOutOfBoundsException | NullPointerException e) { ... } 5.如果异常之间具有父子关系一定是子类异常在前catch父类异常在后catch否则语法错误 public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();int[] arraynull;try{System.out.println(num1/num2);System.out.println(array.length);//空指针异常}catch(Exception e){}catch(ArithmeticException e){System.out.println(存在输入异常);e.printStackTrace();}catch(NullPointerException e){e.printStackTrace();System.out.println(空指针异常);}}    修改如下 public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();int[] arraynull;try{System.out.println(num1/num2);System.out.println(array.length);//空指针异常}catch(ArithmeticException e){System.out.println(存在输入异常);e.printStackTrace();}catch(NullPointerException e){e.printStackTrace();System.out.println(空指针异常);}catch(Exception e){}} 建议从上往下先捕捉子类异常后捕捉父类异常。 2.3.3 finally 在写程序时有些特定的代码不论程序是否发生异常都需要执行比如程序中打开的资源网络连接、数据库连接、IO流等在程序正常或者异常退出时必须要对资源进进行回收。另外因为异常会引发程序的跳转可能导致有些语句执行不到finally就是用来解决这个问题的 语法格式 try{// 可能会发生异常的代码 }catch(异常类型 e){// 对捕获到的异常进行处理 }finally{// 此处的语句无论是否发生异常都会被执行到 } // 如果没有抛出异常或者异常被捕获处理了这里的代码也会执行 public static void main(String[] args) {try{int[] arr {1,2,3};arr[100] 10;arr[0] 10;}catch (ArrayIndexOutOfBoundsException e){System.out.println(e);}finally {System.out.println(finally中的代码一定会执行);} System.out.println(如果没有抛出异常或者异常被处理了try-catch后的代码也会执行);} 问题既然 finally 和 try-catch-finally 后的代码都会执行那为什么还要有finally呢 需求实现getData方法内部输入一个整形数字然后将该数字返回并再main方法中打印   public class TestFinally {public static int getData() {Scanner sc null;try {sc new Scanner(System.in);int data sc.nextInt();return data;} catch (InputMismatchException e) {e.printStackTrace();} finally {System.out.println(finally中代码);}System.out.println(try-catch-finally之后代码);if (null ! sc) {sc.close();}return 0;}public static void main(String[] args) {int data getData();System.out.println(data);} } //正常输入时程序运行结果 100 finally中代码 100上述程序如果正常输入成功接收输入后程序就返回了try-catch-finally之后的代码根本就没有执行即输入流就没有被释放造成资源泄漏。注意finally中的代码一定会执行的一般在finally中进行一些资源清理的扫尾工作   public static void main(String[] args) {Scanner inputnew Scanner(System.in);System.out.println(请输入两个整数);//ArithmeticExceptionint num1input.nextInt();int num2input.nextInt();int[] arraynull;try{System.out.println(num1/num2);System.out.println(array.length);//空指针异常}catch(ArithmeticException e){System.out.println(存在输入异常);e.printStackTrace();}catch(NullPointerException e){e.printStackTrace();System.out.println(空指针异常);}catch(Exception e){}finally{input.close();//Scanner用完需要关闭}} finally 执行的时机是在方法返回之前(try 或者 catch 中如果有 return 会在这个 return 之前执行 finally). 但是如果finally 中也存在 return 语句, 那么就会执行 finally 中的 return, 从而不会执行到 try 中原有的 return。 一般我们不建议在 finally 中写 return (被编译器当做一个警告)【面试题】 1. throw 和 throws 的区别 throw是抛出一个异常throws表示该方法声明有可能抛出异常 2. finally中的语句一定会执行吗 必然执行 2.4异常的处理流程 关于 调用栈 方法之间是存在相互调用关系的, 这种调用关系我们可以用 调用栈 来描述. 在 JVM 中有一块内存空间称为虚拟机栈 专门存储方法之间的调用关系. 当代码中出现异常的时候, 我们就可以使用 e.printStackTrace(); 的方式查看出现异常代码的调用栈. 如果本方法中没有合适的处理异常的方式, 就会沿着调用栈向上传递 public static void func(int a){System.out.println(10/a);}public static void main(String[] args) {func(0);} //先调用main方法 如果向上一直传递都没有合适的方法处理异常, 最终就会交给 JVM 处理, 程序就会异常终止(和我们最开始未使用 try catch 时是一样的) public static void main(String[] args) {func();System.out.println(after try catch);}public static void func() {int[] arr {1, 2, 3};System.out.println(arr[100]);} // 执行结果Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 100at demo02.Test.func(Test.java:14)at demo02.Test.main(Test.java:8) 可以看到, 程序已经异常终止了, 没有执行到 System.out.println(after try catch); 这一行。 【异常处理流程总结】 程序先执行 try 中的代码如果 try 中的代码出现异常, 就会结束 try 中的代码, 看和 catch 中的异常类型是否匹配.如果找到匹配的异常类型, 就会执行 catch 中的代码如果没有找到匹配的异常类型, 就会将异常向上传递到上层调用者.无论是否找到匹配的异常类型, finally 中的代码都会被执行到(在该方法结束之前执行).如果上层调用者也没有处理的了异常, 就继续向上传递一直到 main 方法也没有合适的代码处理异常, 就会交给 JVM 来进行处理, 此时程序就会异常终止三、自定义异常类 Java 中虽然已经内置了丰富的异常类, 但是并不能完全表示实际开发中所遇到的一些异常此时就需要维护符合我们实际情况的异常结构. 例如, 我们实现一个用户登陆功能. public class LogIn {private String userName admin;private String password 123456;public static void loginInfo(String userName, String password) {if (!this.userName.equals(userName)) {System.out.println(用户名错误);} if(!this.password.equals(password)) {System.out.println(密码错误); } System.out.println(登陆成功);}public static void main(String[] args) {LogIn lgnew LogIn();lg.loginInfo(admin, 123456);}} 此时我们在处理用户名密码错误的时候可能就需要抛出两种异常. 我们可以基于已有的异常类进行扩展(继承), 创建和我们业务相关的异常类 具体方式 1. 自定义异常类然后继承自Exception 或者 RunTimeException 2. 实现一个带有String类型参数的构造方法参数含义出现异常的原因 如果我们继承受查异常需要我们自己进行处理--try-catch class UserNameException extends Exception {public UserNameException(String message) {super(message);}}class PasswordException extends Exception {public PasswordException(String message) {super(message);}}public class LogIn {private String userName admin;private String password 123456;public static void loginInfo(String userName, String password)throws UserNameException,PasswordException{if (!userName.equals(userName)) {throw new UserNameException(用户名错误);} if(!password.equals(password)) {throw new PasswordException(用户名错误);} System.out.println(登陆成功);}public static void main(String[] args) {try {loginInfo(admin, 123456);} catch (UserNameException e) {e.printStackTrace();} catch (PasswordException e) {e.printStackTrace();}}} 如果我们继承非受查异常此时不需要自己处理 class UserNameException extends Exception {public UserNameException(String message) {super(message);}}class PasswordException extends RuntimeExceptionException {public PasswordException(String message) {super(message);}}public class LogIn {private String userName admin;private String password 123456;public static void loginInfo(String userName, String password)throws UserNameException,PasswordException{if (!userName.equals(userName)) {throw new UserNameException(用户名错误);} if(!password.equals(password)) {throw new PasswordException(用户名错误);} System.out.println(登陆成功);}public static void main(String[] args) {loginInfo(admin, 123456);}} 注意事项 自定义异常通常会继承自 Exception 或者 RuntimeException继承自 Exception 的异常默认是受查异常继承自 RuntimeException 的异常默认是非受查异常
http://www.hkea.cn/news/14354982/

相关文章:

  • 平台网站建设公司哪家好中文购物网站模板
  • 建立企业网站需要什么番禺大石做网站
  • 作网站流程建房子找哪个网站设计
  • 增城网站建设价格网页设计网站模板素材
  • 深圳网站制作公司售后wordpress 显示 链接深度
  • 做网站站长累吗汕头网站推广找谁
  • 网站建设在线建站网站qq获取
  • 有教做素食的网站吗wordpress读书笔记插件
  • 浦口区城乡建设集团网站作文库网站
  • 自己在网站开发的客户怎么联系上海大型企业名单
  • 网站建设公司推荐万维科技国内十大搜索引擎网站
  • 新网站做seo用ps制作黑色高光网站按钮
  • 建设银行哪个是假网站海口网站建设介绍
  • 在线购物网站的设计平台推广应用
  • 建站公司 phpwindwordpress 树形分类
  • 商丘市做网站c#做的网站怎么上传
  • 烟台优化网站排名什么程序做网站收录好
  • 大兴模版网站建设哪家好大连中小网站建设公司
  • 房产网站建设方案项目书微信公众号做网站卖东西
  • 网站制作计划书模板wordpress 文章数据表
  • 网站上设置返回首页的超链接咋做的seo国外推广软件
  • 芙蓉区乡建设局网站最容易做的网站类型
  • 做箱包外贸哪个网站好高清做视频在线观看网站
  • 网站广告怎么赚钱昆明做网站费用
  • 手机建网站步骤软件网页设计与制作黑马程序员电子版
  • 营销网站的概念在线资源链接
  • 工程建设信息网站网站建设的毕业报告
  • 手机网站首页怎么做互联网技术英文
  • 怎么自己的电脑做网站大鹏网络网站建设
  • 怎么给网站做手机端免费网站添加站长统计