异常的捕获及处理
n异常的概念:
在Java的异常处理机制中,引进了很多用来描述和处理异常的类,称为异常类。异常类定义中包含了该类异常的信息和对异常进行处理的方法。
在Java中,将异常情况分为Exception(异常)和Error(错误)两大类。
异常指程序运行过程中出现的非正常现象,例如用户输入错误、除数为零、需要处理的文件不存在、数组下标越界等。
所谓异常处理,就是指程序在出现问题时依然可以正确的执行完
n异常类的层次和主要子类
异常类(java.lang.Exception)继承于java.lang.Object中的java.lang.Throwable类。异常可分为执行异常(RuntimeException)和检查异常(Checked Exceptions)两种 :
Ø执行异常
继承于RuntimeException
Ø检查异常
除了执行异常外,其余的Exception子类属于检查异常类
Throwable
Error
Exception
Object
AWTException
RuntimeException
IOException
……
执行异常及其用途
异常
说明
RuntimeException
java.lang包中多数异常的基类
ArithmeticException
算术错误,如除以0
IllegalArgumentException
方法收到非法参数
ArrayIndexOutOfBoundsException
数组下标出界
StringIndexOutOfBoundsException
字符串越界
NumberFormatException
String转换为指定的数字类型
NullPointerException
试图访问null对象引用
SecurityException
试图违反安全性
ClassNotFoundException
不能加载请求的类
检查异常及其用途
异常
说明
AWTException
AWT中的异常
IOException
I/O异常的根类
FileNotFoundException
不能找到文件
EOFException
文件结束
IllegalAccessException
对类的访问被拒绝
NoSuchMethodException
请求的方法不存在
InterruptedException
线程中断
n异常处理格式
异常处理方法有两种:
一种方法是使用try…catch…finally结构对异常进行捕获和处理;
另一种方法是通过throws和throw抛出异常。
Ø捕获异常try-catch-finally
Java的异常处理是通过3个关键词来实现的:try-catch-finally。用try来执行一段程序,如果出现异常,系统抛出(throws)一个异常,可以通过它的类型来捕捉(catch)并处理它,最后一步是通过finally语句为异常处理提供一个统一的出口,finally所指定的代码都要被执行(catch语句可有多条;finally语句最多只能有一条,根据自己的需要可要可不要)。
try{
可能出现异常的语句
}
catch(异常类异常对象)
{
异常处理
}
catch(异常类异常对象)
{
异常处理
}
......
finally
{
异常的出口;
}
1、try语句
捕获异常的第一步就是用try {…}语句指定了一段代码,该段代码就是一次捕获并处理异常的范围。在执行过程中,该段代码可能会产生并抛弃一个或多个异常,因此,它后面的catch语句进行捕获时也要做相应的处理。
2、catch语句
每个try语句必须伴随一个或多个catch语句,用于捕获try代码块所产生的异常并做相应的处理。catch语句有一个形式参数,用于指明其所能捕获得异常类型,运行时系统通过参数值把被抛弃的异常对象传递给catch语句
3、finally语句
捕获异常的最后一步是通过finally语句为异常处理提供一个统一的出口,使得在控制流程转到程序的其他部分以前,能够对程序的状态作统一的管理, finally所指定的代码都要被执行
public class ExceptionDemo01
{public static void main(String[] args)
{
int i = 10 ;
int j = 0 ;
System.out.println("==========计算开始===========") ;
try{
int temp = i / j ;
System.out.println("计算结果:" + temp) ;
}
catch(ArithmeticException e){
System.out.println("出现了数学异常:" + e) ;
}
System.out.println("==========计算结束===========") ;
}
}
public class ExceptionDemo02
{public static void main(String[] args)
{
int i = 0 ;
int j = 0 ;
System.out.println("==========计算开始===========") ;
try{
i = Integer.parseInt(args[0]) ;
j = Integer.parseInt(args[1]) ;
int temp = i / j ;
System.out.println("计算结果:" + temp) ;
}catch(ArithmeticException e){
System.out.println("出现了数学异常:" + e) ;
}catch(NumberFormatException e){
System.out.println("输入的不是数字:" + e) ;
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入的参数个数不对:" + e) ;
}finally{
System.out.println("不管是否有异常,我都执行。") ;
}
System.out.println("==========计算结束===========") ;
}
}
Ø通过throws和throw抛出异常
throws方法:
throws关键字的最大好处是:在方法中不处理任何的异常,而交给被调用处处理。
格式:
类型 方法名(参数) throws异常类
如:public int read() throws IOException { … }
throws子句中可以同时指明多个异常,说明该方法将不对这些异常进行处理,而是声明抛弃它们
class Math{
public int div(int i,int j) throws ArithmeticException //除数为0异常
{
return i / j ;
}
}
public class ExceptionDemo03{
public static void main(String args[]){
Math m = new Math() ;
try{
int temp = m.div(10,0) ;
System.out.println(temp) ;
}catch(Exception e){
e.printStackTrace() ;//打印异常
}
}
}
throw方法
在程序中可以使用throw关键字人为的抛出一个异常。
在异常处理中,实际上每次产生异常的时候都是产生了一个异常类的实例化对象。那么此时,也可以通过抛出异常对象的方式完成。
public class ExceptionDemo04{
public static void main(String args[]){
try{
throw new Exception("抛着玩的。") ;//人为抛出
}catch(Exception e){
System.out.println(e) ;
}
}
};
自定义异常类:
import java.io.*;
class MyException extends Exception //创建自定义异常类,继承Exception类
{public MyException(String ErrorMessagr)//构造方法
{ super(ErrorMessagr); }//父类构造方法
}
public class ExceptionDemo05
{static int avg(int number1, int number2)throws MyException //定义方法,抛出异常
{
if (number1 < 0 || number2 < 0)//判断方法中参数是否满足指定条件
throw new MyException("不可以使用负数");//错误信息
if (number1 > 10000 || number2 > 10000) //判断方法中参数是否满足指定条件
throw new MyException("数值太大了");//错误信息
return (number1 + number2) / 2;//将参数的平均值返回
}
public static void main(String args[])throws IOException
{
try {//try代码块处理可能出现异常的代码
int result = avg(-3000, 8500);//调用avg()方法
System.out.println("平均工资:"+result);//将avg()方法的返回值输出
}
catch (MyException e)
{ System.out.println(e) ;}//输出异常信息
}
}
public class ExceptionDemo06{
public static void main(String args[]){
int i = 0 ;
int j = 0 ;
System.out.println("==========计算开始===========") ;
try{
i = Integer.parseInt(args[0]) ;
j = Integer.parseInt(args[1]) ;
int temp = i / j ;
System.out.println("计算结果:" + temp) ;
}catch(ArithmeticException e){
System.out.println("出现了数学异常:" + e) ;
}catch(NumberFormatException e){
System.out.println("输入的不是数字:" + e) ;
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("输入的参数个数不对:" + e) ;
}catch(Exception e){
System.out.println("其他异常:" + e) ;
}finally{
System.out.println("**不管是否有异常,我都执行。") ;
}
System.out.println("==========计算结束===========") ;
}
};
注意:捕获更细的异常要放在捕获更粗的异常之前。
分享到:
2011-10-29 10:05
浏览 50528
评论
1 楼
我没名字30
2017-04-01