目录
3.声明(throws 声明异常 一般情况下用在你的方法头 )
一 .了解异常
1.什么是异常:
程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常。
2.异常体系图:
二.异常的分类
根据Javac对异常的处理要求,将异常类分为2类。
非检查异常(unckecked exception)(运行时异常):Error 和 RuntimeException 以及他们的子类。javac在编译时,不会提示和发现这样的异常,不要求在程序处理这些异常。所以如果愿意,我们可以编写代码处理(使用try...catch...finally)这样的异常,也可以不处理。对于这些异常,我们应该修正代码,而不是去通过异常处理器处理 。这样的异常发生的原因多半是代码写的有问题。如除0错误ArithmeticException,错误的强制类型转换错误ClassCastException,数组索引越界ArrayIndexOutOfBoundsException,使用了空对象NullPointerException等等。
检查异常(checked exception)(非运行时异常):除了Error 和 RuntimeException的其它异常。javac强制要求程序员为这样的异常做预备处理工作(使用try...catch...finally或者throws)。在方法中要么用try-catch语句捕获它并处理,要么用throws子句声明抛出它,否则编译不会通过。这样的异常一般是由程序的运行环境导致的。因为程序可能被运行在各种未知的环境下,而程序员无法干预用户如何使用他编写的程序,于是程序员就应该为这样的异常时刻准备着。如SQLException , IOException,ClassNotFoundException 等。
三. 异常的处理方式
1 .捕获
Try ---catch ----finally
try 块{
//可能会有异常发生的代码
}catch(Exception e){
// 当有异常发生的时候,catch块被执行
当没有异常发生的时候 catch块就不执行了
}finally{
不管异常是否发生都要执行
}
try-catch-finally 的执行顺序:
- try块中有return关键字时 如果没有finally 则直接返回
- try块中有return 关键字 catch块也有return 如有有异常时则作走catch块的return
如果没有异常则走try里的return
- 当try块中有return finally中也有return 如果没有异常发生走finally里的return
- 当try块中有return catch也有return finally还有return
没异常按照第3条走,如果有异常 则按照finally中的返回结果执行。
public class Test {
public static void main(String[] args) {
Test t = new Test();
Test tt =t.get();
System.out.println(tt);
}
public Test get(){
int a = 10;
int b = 0;
try{
int c = 10/0;
System.out.println("444");
return null;
}catch(Exception e){
try{
System.out.println("123");
}catch(Exception ex){
}
return this;
}finally{
System.out.println("我是一定要执行的");
return null;
}
2.抛出(throw)
public static void main(String[] args) {
//抛出异常throw
String id;
System.out.println("请输入您的员工编号");
Scanner input = new Scanner(System.in);
id = input.next();
if(id.length() == 6){
System.out.println("你是一个合格的员工");
}else{
throw new IllegalArgumentException("你的id不符合要求,请联系上帝");
}
}
3.声明(throws 声明异常 一般情况下用在你的方法头 )
public boolean createNewFile() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.createFileExclusively(path);
}
区分:
throw和throws的区别?
throw 抛出 用于抛出一些运行时异常 在代码中可以自定义逻辑 也可以抛出自定义的异常
throws 声明异常 用于方法异常声明 主要是声明非运行时异常,做检查使用
4.自定义异常
Exception RunTimeException
用于在自己的框架中描述一些自定义的异常类,展示和Java原有的异常信息不同
实现方案:
要借助 Exception 和 RunTimeException
public class MyDaoException extends RuntimeException {
static final long serialVersionUID = -7034897190745766939L;
public MyDaoException() {
super();
}
public MyDaoException(String message) {
super(message);
}
public MyDaoException(Throwable cause) {
super(cause);
}
public MyDaoException(String message, Throwable cause) {
super(message, cause);
}
}
5.多个异常处理
在多个catch块中 异常处理的顺序 要遵循:
先小后大, 异常的抛出要相对准确,不建议捕获异常的父类级别的异常类例如想Exception或者Throwable 以及RuntimeException 如果能够进准到具体的哪个异常可以大大地增加代码的可读性和排错性。
public class Test2 {
public static void main(String[] args) {
try{
String key = null;
char d = key.charAt(0);
System.out.println(d);
int a = 10;
int b = 0;
int c = 10/0;
}catch(NullPointerException e){
}catch(ArithmeticException e){
}catch(Exception e){
}
}
}