java异常处理.docx

上传人:b****6 文档编号:5608290 上传时间:2022-12-29 格式:DOCX 页数:24 大小:69.60KB
下载 相关 举报
java异常处理.docx_第1页
第1页 / 共24页
java异常处理.docx_第2页
第2页 / 共24页
java异常处理.docx_第3页
第3页 / 共24页
java异常处理.docx_第4页
第4页 / 共24页
java异常处理.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

java异常处理.docx

《java异常处理.docx》由会员分享,可在线阅读,更多相关《java异常处理.docx(24页珍藏版)》请在冰豆网上搜索。

java异常处理.docx

java异常处理

1.纲要

1.异常的基本概念

2.异常的分类

3.异常的捕获和处理

4.自定义异常

5.方法覆盖与异常

2、内容

1.12.1、异常的基本概念

什么是异常,在程序运行过程中出现的错误,称为异常

publicclassExceptionTest01{

publicstaticvoidmain(String[]args){

inti1=100;

inti2=0;

inti3=i1/i2;

System.out.println(i3);

}

}

没有正确输出,抛出了被0除异常

通过以上示例,我们看到java给我们提供了这样一个体系结构,当出现问题的时候,它会告诉我们,并且把错误的详细信息也告诉我们了,这就是异常的体系结构,这样我们的程序更健壮,我们可以把这个信息,再进行处理以下告诉用户。

从上面大家还可以看到,java异常都是类,在异常类中会携带一些信息给我们,我们可以通过这个类把信息取出来

1.22.2、异常的分类

1.2.12.2.1、异常的层次结构

1.2.22.2.2、异常的分类

异常主要分为:

错误、一般性异常(受控异常)、运行期异常(非受控异常)

●错误:

如果应用程序出现了Error,那么将无法恢复,只能重新启动应用程序,最典型的Error的异常是:

OutOfMemoryError

●受控异常:

出现了这种异常必须显示的处理,不显示处理java程序将无法编译通过

●非受控异常:

此种异常可以不用显示的处理,例如被0除异常,java没有要求我们一定要处理

1.2.32.3.1、try、catch和finally

异常的捕获和处理需要采用try和catch来处理,具体格式如下:

try{

}catch(OneExceptione){

}catch(TwoExceptione){

}finally{

}

●try中包含了可能产生异常的代码

●try后面是catch,catch可以有一个或多个,catch中是需要捕获的异常

●当try中的代码出现异常时,出现异常下面的代码不会执行,马上会跳转到相应的catch语句块中,如果没有异常不会跳转到catch中

●finally表示,不管是出现异常,还是没有出现异常,finally里的代码都执行,finally和catch可以分开使用,但finally必须和try一块使用,如下格式使用finally也是正确的

try{

}finally{

}

【示例代码】

publicclassExceptionTest02{

publicstaticvoidmain(String[]args){

inti1=100;

inti2=0;

//try里是出现异常的代码

//不出现异常的代码最好不要放到try作用

try{

//当出现被0除异常,程序流程会执行到“catch(ArithmeticExceptionae)”语句

//被0除表达式以下的语句永远不会执行

inti3=i1/i2;

//永远不会执行

System.out.println(i3);

//采用catch可以拦截异常

//ae代表了一个ArithmeticException类型的局部变量

//采用ae主要是来接收java异常体系给我们new的ArithmeticException对象

//采用ae可以拿到更详细的异常信息

}catch(ArithmeticExceptionae){

System.out.println("被0除了");

}

}

}

1.2.42.3.2、getMessage和printStackTrace()

如何取得异常对象的具体信息,常用的方法主要有两种:

⏹取得异常描述信息:

getMessage()

⏹取得异常的堆栈信息(比较适合于程序调试阶段):

printStackTrace();

【代码示例】

publicclassExceptionTest03{

publicstaticvoidmain(String[]args){

inti1=100;

inti2=0;

try{

inti3=i1/i2;

System.out.println(i3);

}catch(ArithmeticExceptionae){

//ae是一个引用,它指向了堆中的ArithmeticException

//通过getMessage可以得到异常的描述信息

System.out.println(ae.getMessage());

}

}

}

【代码示例】

publicclassExceptionTest04{

publicstaticvoidmain(String[]args){

method1();

}

privatestaticvoidmethod1(){

method2();

}

privatestaticvoidmethod2(){

inti1=100;

inti2=0;

try{

inti3=i1/i2;

System.out.println(i3);

}catch(ArithmeticExceptionae){

//ae是一个引用,它指向了堆中的ArithmeticException

//通过printStackTrace可以打印栈结构

ae.printStackTrace();

}

}

}

1.2.52.3.3、受控异常

importjava.io.FileInputStream;

publicclassExceptionTest05{

publicstaticvoidmain(String[]args){

FileInputStreamfis=newFileInputStream("test.txt");

}

}

从上面输出可以看到,无法编译,它抛出了一个异常,这个异常叫做“受控异常”FileNotFoundException,也就是说在调用的时候必须处理文件不能找到

处理FileNotFoundException

/*

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

*/

importjava.io.*;

publicclassExceptionTest06{

publicstaticvoidmain(String[]args){

try{

FileInputStreamfis=newFileInputStream("test.txt");

}catch(FileNotFoundExceptionffe){//此异常为受控异常,必须处理

ffe.printStackTrace();

}

}

}

1.2.62.3.4、finally关键字

finally在任何情况下都会执行,通常在finally里关闭资源

【示例代码】

importjava.io.*;

publicclassExceptionTest07{

publicstaticvoidmain(String[]args){

try{

FileInputStreamfis=newFileInputStream("test.txt");

System.out.println("-------beforefis.close--------");

//close是需要拦截IOException异常

//在此位置关闭存在问题,当出现异常

//那么会执行到catch语句,以下fis.close永远不会执行

//这样个对象永远不会得到释放,所以必须提供一种机制

//当出现任何问题,都会释放相应的资源(恢复到最初状态)

//那么就要使用finally语句块

fis.close();

System.out.println("-------afterfis.close--------");

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

采用finally来释放资源

importjava.io.*;

publicclassExceptionTest08{

publicstaticvoidmain(String[]args){

//因为fis的作用域问题,必须放到try语句块外,局部变量必须给初始值

//因为是对象赋值为null

FileInputStreamfis=null;

try{

//FileInputStreamfis=newFileInputStream("test.txt");

fis=newFileInputStream("test.txt");

/*

System.out.println("-------beforefis.close--------");

fis.close();

System.out.println("-------afterfis.close--------");

*/

}catch(FileNotFoundExceptione){

e.printStackTrace();

}finally{

try{

System.out.println("-------beforefis.close--------");

//放到finally中的语句,程序出现任何问题都会被执行

//所以finally中一般放置一些需要及时释放的资源

fis.close();

System.out.println("-------afterfis.close--------");

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

深入finally

【代码示例】

publicclassExceptionTest09{

publicstaticvoidmain(String[]args){

inti1=100;

inti2=10;

try{

inti3=i1/i2;

System.out.println(i3);

return;

}catch(ArithmeticExceptionae){

ae.printStackTrace();

}finally{

//会执行finally

System.out.println("----------finally---------");

}

}

}

深入finally

publicclassExceptionTest10{

publicstaticvoidmain(String[]args){

inti1=100;

inti2=10;

try{

inti3=i1/i2;

System.out.println(i3);

//return;

System.exit(-1);//java虚拟机退出

}catch(ArithmeticExceptionae){

ae.printStackTrace();

}finally{

//只有java虚拟机退出不会执行finally

//其他任何情况下都会执行finally

System.out.println("----------finally---------");

}

}

}

深入finally

publicclassExceptionTest11{

publicstaticvoidmain(String[]args){

intr=method1();

//输出为:

10

System.out.println(r);

}

/*

privatestaticintmethod1()

{

bytebyte0=10;

bytebyte3=byte0;//将原始值进行了保存

bytebyte1=100;

returnbyte3;

Exceptionexception;

exception;

bytebyte2=100;

throwexception;

}

*/

privatestaticintmethod1(){

inta=10;

try{

returna;

}finally{

a=100;

}

}

}

深入finally

publicclassExceptionTest12{

publicstaticvoidmain(String[]args){

intr=method1();

//输出为:

100

System.out.println(r);

}

/*

privatestaticintmethod1()

{

bytebyte0=10;

byte0=50;

byte0=100;

breakMISSING_BLOCK_LABEL_18;

Exceptionexception;

exception;

byte0=100;

throwexception;

returnbyte0;

}

*/

privatestaticintmethod1(){

inta=10;

try{

a=50;

}finally{

a=100;

}

returna;

}

}

1.2.72.3.5、final、finalize和finally?

1.2.82.3.6、如何声明异常

在方法定义处采用throws声明异常,如果声明的异常为受控异常,那么调用方法必须处理此异常

【示例代码】,声明受控异常

importjava.io.*;

publicclassExceptionTest13{

publicstaticvoidmain(String[]args)

//throwsFileNotFoundException,IOException{//可以在此声明异常,这样就交给java虚拟机处理了,不建议这样使用

throwsException{//可以采用此种方式声明异常,因为Exception是两个异常的父类

/*

//分别处理各个异常

try{

readFile();

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

*/

//可以采用如下方式处理异常

//因为Exception是FileNotFoundException和IOException的父类

//但是一般不建议采用如下方案处理异常,粒度太粗了,异常信息

//不明确

/*

try{

readFile();

}catch(Exceptione){

e.printStackTrace();

}

*/

readFile();

}

privatestaticvoidreadFile()

throwsFileNotFoundException,IOException{//声明异常,声明后调用者必须处理

FileInputStreamfis=null;

try{

fis=newFileInputStream("test.txt");

//}catch(FileNotFoundExceptione){

//e.printStackTrace();

}finally{

//try{

fis.close();

//}catch(IOExceptione){

//e.printStackTrace();

//}

}

}

}

【示例代码】,声明非受控异常

publicclassExceptionTest14{

publicstaticvoidmain(String[]args){

//不需要使用try...catch..,因为声明的是非受控异常

//method1();

//也可以拦截非受控异常

try{

method1();

}catch(ArithmeticExceptione){

e.printStackTrace();

}

}

//可以声明非受控异常

privatestaticvoidmethod1()throwsArithmeticException{

inti1=100;

inti2=0;

//try{

inti3=i1/i2;

System.out.println(i3);

/*

}catch(ArithmeticExceptionae){

ae.printStackTrace();

}

*/

}

}

1.2.92.3.7、如何手动抛出异常

publicclassExceptionTest15{

publicstaticvoidmain(String[]args){

intret=method1(1000,10);

if(ret==-1){

System.out.println("除数为0");

}

if(ret==-2){

System.out.println("被除数必须为1~100之间的数据");

}

if(ret==1){

System.out.println("正确");

}

//此种方式的异常处理,完全依赖于程序的返回

//另外异常处理和程序逻辑混在一起,不好管理

//异常是非常,程序语句应该具有一套完成的异常处理体系

}

privatestaticintmethod1(intvalue1,intvalue2){

if(value2==0){

return-1;

}

if(!

(value1>0&&value1<=100)){

return-2;

}

intvalue3=value1/value2;

System.out.println("value3="+value3);

return1;

}

}

采用异常来处理参数非法

publicclassExceptionTest16{

publicstaticvoidmain(String[]args){

//intret=method1(10,2);

//System.out.println(ret);

/*

try{

intret=method1(1000,10);

System.out.println(ret);

}catch(IllegalArgumentExceptioniae){

//ide为指向堆中的IllegalArgumentException对象的地址

System.out.println(iae.getMessage());

}

*/

try{

intret=method1(1000,10);

System.out.println(ret);

}catch(Exceptioniae){//可以采用Exception拦截所有的异常

System.out.println(iae.getMessage());

}

}

privatestaticintmethod1(intvalue1,intvalue2){

if(value2==0){

////手动抛出异常

thrownewIllegalArgumentException("除数为0");

}

if(!

(value1>0&&value1<=100)){

//手动抛出异常

thrownewIllegalArgumentException("被除数必须为1~100之间的数据");

}

intvalue3=value1/value2;

returnvalue3;

}

}

throws和throw的区别?

thorws是声明异常,throw是抛出异常

进一步了解throw

publicclassExceptionTest17{

publicstaticvoidmain(String[]args){

try{

intret=method1(1000,10);

System.out.println(ret);

}catch(Exceptioniae){

System.out.println(iae.getMessage());

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 职高对口

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1