您的位置:首页 > 其它

异常概念和处理机制,try-catch-finally,throw和throws,自定义异常

2016-03-29 23:43 597 查看
异常概念和处理机制

什么是异常?

所谓异常就是指在程序运行的过程中发生的一些不正常事件。(如除0溢出,数组下标越界,所要读取的文件不存在);

异常导致的后果?

Java程序的执行过程中如出现异常事件,可以生成一个异常类对象,该异常对象封装了异常事件的信息,其将被提交给Java运行时系统,这个过程称为抛出异常,不处理的话会导致程序直接中断;

如何防止程序中断?

设计良好的程序应该在程序异常发生时提供处理这些异常的方法,使得程序不会因为异常的发生而阻断或产生不可预见的结果;

异常分类





异常类型

包括

来源

处理

受查异常

checkedException

Exception及其子类(不包括

RuntimeException

及其子类)

由代码控制能力之外的因素导致额运行时错误

必须要处理,否则通不过编译

非受查异常

uncheckedException

Error和

RuntimeException

及其子类

RuntimeException

一般代表编译错误

可以不用处理

Java的异常通过两种机制来处理

捕获:try-catch-finally

抛出:throw,throws



try-catch-finally

try{}语句块中放的是要检测的java代码,可能有会抛出异常,也可能会正常执行;

catch(异常类型){}块是当Java运行时系统接收到try块中所抛出的异常对象时,会寻找能处理这一异常的catch块来进行处理(可以有多个catch块);

finally{}块不管系统有没有抛出异常都会去执行,一般用来释放资源。除了在之前执行了System.exit(0);



import java.util.Scanner;
import java.util.InputMismatchException;
public class ExceptionDemo1{
public static void main(String []args){
System.out.println("请输入一个数字");
Scanner input=new Scanner(System.in);
int res=0;
try{
//return;//添加return之后,还是会执行finally语句块
//System.exit(0);//添加之后不会执行finally语句块
int number=input.nextInt();
res=10/number;
}catch(InputMismatchException e){
//输入字母时错误
//错误信息
System.out.println(e.getMessage());
//堆栈信息
e.printStackTrace();
}catch(ArithmeticException e){
//输入0时错误
//错误信息
System.out.println(e.getMessage());
//堆栈信息
e.printStackTrace();
}catch(Exception e){
//该异常为父类,若不清楚是何异常,可以使用该类
//若都使用时,应该先使用子类再用该类
System.out.println(e.getMessage());
e.printStackTrace();
}
finally{
//释放资源,比如关闭打开的文件,删除一些临时文件等
System.out.println("结果为:"+res);
}
}
}


空指针异常

public class ExceptionDemo1{
public static void main(String []args){
//testTryFinally("张三");//输出2 end
testTryFinally(null);//空指针异常
}

public static void testTryFinally(String name){
try{
System.out.println(name.length());
}finally{
System.out.println("end");
}
}
}


throw和throws

throw用于手动抛出异常。作为程序员可以再任意位置手动抛出异常;

throws用于在方法上标识要暴露的异常。抛出的异常交由调用者处理;

两者区别:

① throw用在方法内,后面跟上要抛出的异常类对象;

② throws修饰在方法上,告诉调用者此方法可能会抛出异常,后面跟上要抛出的异常类名;

未处理异常

public class ExceptionDemo2{
public static void main(String []args){
Bar bar=new Bar();
bar.enter(15);
//未打印end
System.out.println("end");
}
}

class Bar{
public void enter(int age){
if(age<18){
//受查异常(必须捕获,否则编译不通过)和非受查异常
throw new IllegalArgumentException("年龄不合格");
}else{
System.out.println("欢迎光临");
}
}
}


在调用方法时处理异常

public class ExceptionDemo2{
public static void main(String []args){
Bar bar=new Bar();
try{
bar.enter(15);
}catch(IllegalArgumentException e){
System.out.println("错误信息:"+e.getMessage());
}
System.out.println("end");
}
}

class Bar{
public void enter(int age){
if(age<18){
//受查异常(必须捕获,否则编译不通过)和非受查异常
throw new IllegalArgumentException("年龄不合格");
}else{
System.out.println("欢迎光临");
}
}
}


方法中未处理异常,但调用者可能不知道抛出什么异常,所有在方法上加上throws,用于方法中未处理异常,交由调用者处理

public class ExceptionDemo2{
public static void main(String []args){
Bar bar=new Bar();
try{
bar.enter(15);
}catch(IllegalArgumentException e){
System.out.println("错误信息:"+e.getMessage());
}
System.out.println("end");
}
}

class Bar{
public void enter(int age)throws IllegalArgumentException{
if(age<18){
//受查异常(必须捕获,否则编译不通过)和非受查异常
throw new IllegalArgumentException("年龄不合格");
}else{
System.out.println("欢迎光临");
}
}
}


受查异常必须要捕获,否则编译不通过

public class ExceptionDemo2{
public static void main(String []args){
Bar bar=new Bar();
//try{
bar.enter(15);
/*}catch(IllegalArgumentException e){
System.out.println("错误信息:"+e.getMessage());
}*/
System.out.println("end");
}
}

class Bar{
public void enter(int age) {
if(age<18){
//受查异常(必须捕获,否则编译不通过)和非受查异常
//throw new IllegalArgumentException("年龄不合格");//非受查异常
throw new Exception("年龄不合格");//受查异常
}else{
System.out.println("欢迎光临");
}
}
}


当抛出的是受查异常时,且方法中不自己捕获异常,必须加上throws,否则编译不通过

public class ExceptionDemo2{
public static void main(String []args){
Bar bar=new Bar();
try{
bar.enter(15);
}catch(Exception e){
System.out.println("错误信息:"+e.getMessage());
}
System.out.println("end");
}
}

class Bar{
public void enter(int age)throws Exception{
if(age<18){
//受查异常(必须捕获,否则编译不通过)和非受查异常
//throw new IllegalArgumentException("年龄不合格");//非受查异常
throw new Exception("年龄不合格");//受查异常
}else{
System.out.println("欢迎光临");
}
}
}


自定义异常

常见异常:

非受查异常RuntimeException

受查异常IOException,SQLException,ClassNotFoundException

自定义异常:Java提供的异常体系不可能预见所有希望加以报告的错误;

自定义异常类必须从已有的异常类继承:用的最多的是Exception;

建立新的异常类型最简单的方法就是让编译器产生默认构造方法;

对异常来说,最重要的部分就是它的类名;

可以为异常类定义一个接受字符串参数的构造方法,字符串参数描述异常信息;

public class ExceptionDemo3{
public static void main(String []args){
Bar bar=new Bar();
try{
bar.enter(15);
}catch(AgeLessThanEighteenException e){
System.out.println("错误信息:"+e.getMessage());
}
System.out.println("end");
}
}

//自定义异常
class AgeLessThanEighteenException extends Exception{
private String message;//描述异常信息
public AgeLessThanEighteenException(String message){
this.message=message;
}
//重写getMessage()方法
public String getMessage(){
return message;
}
}

class Bar{
public void enter(int age)throws AgeLessThanEighteenException{
if(age<18){
throw new AgeLessThanEighteenException("年龄不合格");
}else{
System.out.println("欢迎光临");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: