欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

JAVASE——3.异常

时间:2023-06-13
JAVASE——异常 文章目录

JAVASE——异常

捕获异常finallyIO异常捕获(JDK7之前)IO异常捕获( JDK7 之后)throws/throw 关键字:**throw与throws的比较**throws/throw重写规则异常常用方法自定义异常
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xsvfouvD-1644759423846)(E:达内图片异常1.jpg)]

Java异常可以分为可检测异常,非检测异常。

从继承关系可知:Throwable是异常体系的根,它继承自Object。Throwable有两个体系:Error和Exception,Error表示严重的错误,程序对此一般无能为力,例如:

OutOfMemoryError:内存耗尽NoClassDefFoundError:无法加载某个ClassStackOverflowError:栈溢出

而Exception则是运行时的错误,它可以被捕获并处理。

某些异常是应用程序逻辑处理的一部分,应该捕获并处理。例如:

NumberFormatException:数值类型的格式错误FileNotFoundException:未找到文件SocketException:读取网络失败

还有一些异常是程序逻辑编写不对造成的,应该修复程序本身。例如:

NullPointerException:对某个null的对象调用方法或字段IndexOutOfBoundsException:数组索引越界

Exception又分为两大类:

RuntimeException以及它的子类;非RuntimeException(包括IOException、ReflectiveOperationException等等)

Java规定:

必须捕获的异常,包括Exception及其子类,但不包括RuntimeException及其子类,这种类型的异常称为Checked Exception。不需要捕获的异常,包括Error及其子类,RuntimeException及其子类。

注意下面事项:

catch 不能独立于 try 存在。在 try/catch 后面添加 finally 块并非强制性要求的。try 代码后不能既没 catch 块也没 finally 块。try, catch, finally 块之间不能添加任何代码在写程序时,对可能会出现异常的部分通常要用try{…}catch{…}去捕捉它并对它进行处理;用try{…}catch{…}捕捉了异常之后一定要对在catch{…}中对其进行处理,那怕是最简单的一句输出语句,或栈输入e.printStackTrace();如果是捕捉IO输入输出流中的异常,一定要在try{…}catch{…}后加finally{…}把输入输出流关闭;如果在函数体内用throw抛出了某种异常,最好要在函数名中加throws抛异常声明,然后交给调用它的上层函数进行处理。 捕获异常

捕获异常使用try...catch语句,把可能发生异常的代码放到try {...}中,然后使用catch捕获对应的Exception及其子类:

package exception;public class TryException { public static void main(String[] args) { System.out.println("程序开始了"); try { // String str = null;// String str = "";//这样写就不会走catch// String str = "a"; //这里会出现空指针异常,虚拟机会实例化空指针异常实例并在这里抛出异常NullPointerException System.out.println(str.length()); System.out.println(str.charAt(0)); System.out.println(Integer.parseInt(str)); //当异常处理完毕后,jvm不会返回再执行下面的语句,而是跳出catch执行catch下面的语句 System.out.println("-------------------");//ctrl +/ 可全都注释// }catch(NullPointerException e){// System.out.println("出现了空指针,并在这里得以解决!");// //catch可以定义多个,针对不用的异常有不同处理办法时,可以分别捕获并处理// }catch(StringIndexOutOfBoundsException e){// System.out.println("出现了字符串下标越界了!并在这里解决!"); //可以合并捕获异常,当不同异常处理手段相同时,可以用这种方式 }catch(NullPointerException | StringIndexOutOfBoundsException e){ System.out.println("出现了空指针或下标越界的处理!"); }catch(Exception e){ System.out.println("兜底的,不知道出现什么异常,是一个超类"); } System.out.println("程序结束了"); }}

finally

package exception;public class FinallyDemo { public static void main(String[] args) { System.out.println("程序开始了"); //ctrl + alt + t try { String str = null; System.out.println(str.length()); //return;加上此语句后,finally下面的代码不会执行,因此要加上finally保证代码的执行 }catch (Exception e) {// System.err.println("!!!!!");// System.out.println("出错了");// //e.printStackTrace(); }finally{ //无论try中是否有异常,finally一定会执行 System.out.println("finally中的代码执行了"); } System.out.println("程序结束了"); }}

IO异常捕获(JDK7之前)

package exception;import java.io.FileOutputStream;import java.io.IOException;public class FinallyDemo2 { public static void main(String[] args) { FileOutputStream fos = null; try{ fos = new FileOutputStream("fos.dat"); fos.write(1); } catch (IOException e) { System.out.println("出错了!在这里解决了!"); }finally{ try{ if(fos!=null){ fos.close(); } }catch(IOException e){ e.printStackTrace(); } } }}

IO异常捕获( JDK7 之后)

package exception;import java.io.FileOutputStream;import java.io.IOException;public class AutoCloseDemo { public static void main(String[] args) { try( FileOutputStream fos = new FileOutputStream("fos.dat"); ){ fos.write(2); }catch (IOException e){ System.out.println("出错了"); } }}

throws/throw 关键字:

throw是语句抛出一个异常,一般是在代码块的内部,当程序出现某种逻辑错误时由程序员主动抛出某种特定类型的异常。throw用来对外主动抛出一个异常,通常下面两种情况我们主动对外抛出异常:

1:当程序遇到一个满足语法,但是不满足业务要求时,可以抛出一个异常告知调用者。

2:程序执行遇到一个异常,但是该异常不应当在当前代码片段被解决时可以抛出给调用者。

throw new ArithmeticException();

throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常)

throws关键字

当一个方法中使用throw抛出一个非RuntimeException的异常时,就要在该方法上使用throws声明这个异常的抛出。此时调用该方法的代码就必须处理这个异常,否则编译不通过。

当我们调用一个含有throws声明异常抛出的方法时,编译器要求我们必须处理这个异常,否则编译不通过。 处理手段有两种:

使用try-catch捕获并处理这个异常在当前方法(本案例就是main方法)上继续使用throws声明该异常的抛出给调用者解决。 具体选取那种取决于异常处理的责任问题。

package exception;public class Person { private int age;//年龄 public int getAge() { return age; } public void setAge(int age) throws Exception{ if(age < 0 || age > 100){// throw new RuntimeException("年龄不合法!"); //PS:虽然使用try-catch也能保证不报错,但是这里没意义! throw new Exception("年龄不合法"); } this.age = age; }}package exception;public class ThrowDemo { public static void main(String[] args) { Person p = new Person(); try{ p.setAge(10000);//满足语法,但是不满足业务需求 }catch (Exception e){ e.printStackTrace(); } System.out.println("此人年龄"+p.getAge()); }}

throw与throws的比较

throws出现在方法函数头;而throw出现在函数体。throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某种异常对象。两者都是消极处理异常的方式(这里的消极并不是说这种方式不好),只是抛出或者可能抛出异常,但是不会由函数去处理异常,真正的处理异常由函数的上层调用处理。 throws/throw重写规则

package exception;import java.awt.*;import java.io.IOException;public class ThrowsDemo { public void dosome()throws IOException,AWTException{}}class SubClass extends ThrowsDemo{ // public void dosome() throws IOException,AWTException{} //允许抛出部分异常// public void dosome()throws IOException{} //允许不再抛出任何异常// public void dosome(){} //允许抛出超类方法抛出的子类型异常// public void dosome()throws FileNotFoundException{} //不允许抛出额外异常(超类方法没有声明抛出的,或和超类声明抛出的异常没有继承关系的)// public void dosome()throws SQLException{} //不允许抛出超类方法抛出异常的超类型异常// public void dosome()throws Exception{} }

异常常用方法

package exception;public class ExceptionApiDemo { public static void main(String[] args) { try { String str = "abc"; System.out.println(Integer.parseInt(str));//NumberFormatException } catch (Exception e) { //该方法用来在控制台输出异常的堆栈信息,便于程序员debug e.printStackTrace(); //message:消息 String message = e.getMessage();//获取错误信息 //message一般用于给用户进行提示,或者记录log日志时使用。 System.out.println(message); } }}

自定义异常

package exception;public class IllegalAgeException extends Exception{ public IllegalAgeException() { } public IllegalAgeException(String message) { super(message); } public IllegalAgeException(String message, Throwable cause) { super(message, cause); } public IllegalAgeException(Throwable cause) { super(cause); } public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); }}package exception;public class Person { private int age;//年龄 public int getAge() { return age; } public void setAge(int age) throws IllegalAgeException{ if(age<0 | age >100){ //PS:虽然使用try-catch也能保证不报错,但是这里没意义! // throw new Exception("输入的年龄不合法"); throw new IllegalAgeException("年龄超过了范围"+age); } this.age = age; }}package exception;public class ThrowDemo { public static void main(String[] args) { Person p = new Person(); try{ p.setAge(10000); }catch(IllegalAgeException e){ e.printStackTrace(); } System.out.println("此人年龄"+p.getAge()); }}

ic static void main(String[] args) {
Person p = new Person();

try{
p.setAge(10000);
}catch(IllegalAgeException e){
e.printStackTrace();
}
System.out.println(“此人年龄”+p.getAge());
}
}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。