null第五章 异常第五章 异常nullJava中常用的数据结构有哪些?
什么情况下使用Map?
目标目标异常的基本概念
异常的分类
异常类的层次结构
捕获异常
声明异常
抛出异常
创建自己的异常 异常基本概念 异常基本概念 异常:在程序运行时打断正常程序流程的任何不正常的
情况称为错误或异常。
Java语言用面向对象的方法通过异常处理机制来处理错误观察以下例子的输出观察以下例子的输出例1: exception\
public class ExceptionDemo1{
public static void main(String args[]){
int a=0;
System.out.println(8/a);
}
} java.lang.ArithmeticException java.lang.ArrayIndexOutOfBoundsException例2:exception\
public class ExceptionDemo2{
public static void main(String []args){
int []arr=new int[5];
arr[5]=10; //数组下标越界
System.out.println("Exception Demo");
}
} java.lang.ArrayIndexOutOfBoundsException java.lang.NumberFormatException例3:exception\
public class ExceptionDemo3{
public static void main(String args[]){
String str="222.34 a 56 ";
Double i=new Double(str);
System.out.println(i);
}
} java.lang.NumberFormatExceptionnull异常可分为两大类型:
Error类:由Java虚拟机生成并抛出,Java程序不做处理.
Exception类:(程序中的问题,可预知的),标准Java库方法所激发的异常,通过某种修正后程序还能继续运行。
Java中的异常机制主要针对Exception类进行。
Java异常体系Java异常体系Java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.ArithmeticException、IndexOutOfBoundsException
java.lang.NullPointerException
java.lang. IOException
java.lang.EOFException、FileNotFoundException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.Error
如果不对程序的异常情况进行处理,将会导致程序不正常终止。
为了保证程序的正常运行,Java专门提供了异常处理机制。null利用Exception类的方法获取更多的信息。
(1)public String toString():
返回描述当前异常对象信息的字符串。
(2)public String getMessage():
返回描述当前异常对象信息的详细信息。
(3)public void printStackTrace():
没有返回值,屏显当前异常对象使用堆栈的轨迹,
即程序先后调用了哪些方法,使得运行过程产生了这个异常对象。
异常处理方法异常处理方法Java语言中,异常的处理有以下几种方式:
(1)、捕获并处理异常:使用try~catch~finally语句。
(2)、将方法中的异常抛出:使用throw语句直接抛出异常
或使用throws语句间接抛出异常。异常处理机制 异常处理机制 异常处理机制执行过程:
出现异常事件时,Java系统自动产生一个异常对象,然后
将这个对象传递给Java运行时系统,这个例外产生和提交的过
程称为抛出异常(throw);
当Java运行时系统得到例外对象以后,它将会寻找处理这
一例外的代码,找到能处理这一例外的方法以后,运行时系统
把当前例外对象交给这个方法进行处理,这一过程称为捕获异
常(catch)。
一、捕获并处理异常 try~catch~finally语句一、捕获并处理异常 try~catch~finally语句 语法结构:
try
{
可能产生异常的代码段;
}catch(异常类名1 对象名1){
处理语句组1; }catch(异常类名2 对象名2){
处理语句组2;
……
}finally{
最终处理语句;
}null例4:处理除数为0的异常 ,注意 异常语句的执行过程。例:exception\
public class UserExceptionDemo1{
public static void main(String args[]){
int a,b;
try{
a=10;b=0;
int c=Divide(a,b);
System.out.println(a+"/"+b+"="+c);
}catch(ArithmeticException e){
System.out.println(“捕获被零除异常!");
}finally{
System.out.println(“进入finally 处理模块");
}
System.out.println("After try-catch.");
}
static int Divide(int x,int y){
return x/y;
}
}100/a[0]=10
Divided by zero!
in finally block
After try-catch.100/a[0]=10
Divided by zero!
in finally block
After try-catch.例5: 处理数组下标越界的异常,使用多个catch语句。exception\
public class MulCatchTest{
public static void main(String []args){
//nt a[]=new int[2]; int a[]=new int[1];
int n=a.length; int x; a[0]=10;
try{
for(int i=0;i<=n;i++){
x=100/a[i];
System.out.println("100/a["+i+"]="+x);
}
}catch(Exception e){
System.out.println("捕获被零除异常!“+e.getMessage());
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(“数组下标越界异常!“+e.getMessage());
} finally{
System.out.println("in finally block");
}
System.out.println("After try-catch.");
}
}这道题有错误,请指出ArithmeticExceptiontry~catch~finally语句说明try~catch~finally语句说明(1)若try没有异常,则try~finally
(2)若try发生异常,并在catch中有匹配,则跳过try中剩余的语句,转去执行catch,执行完catch后,程序继续执行。 则try~catch~finally
(3)若catch中又发生异常,异常抛给方法的调用者。
(4)若try发生异常,没有catch匹配,则try~finally,异常抛给方法调用者。
(5)finally:有无异常都会执行。为异常提供清理机制,一般
用于关闭文件或释放系统资源。
(6)catch如何匹配:
。参数与异常同一个类;
。参数是产生异常的基类;
。参数是Throwable类的子类nullfinally在文件处理时非常有用
try {
对文件进行处理的程序;
}catch(IOException e) {
//对文件异常进行处理;
}finally {
不论是否发生异常,都关闭文件;
}
未捕获异常的处理未捕获异常的处理 如果代码周围没有try/catch子句,或者未找到匹配的catch
子句,则异常就会向上抛至调用方法。如果异常仍未得到捕
获,则进一步抛至更上一层,直至异常得到处理。Divided by zero!
异常在外层被捕获!
Divided by zero!
异常在外层被捕获!
例6 :异常的抛出
public class TryNestDemo{
public static void main(String []args){
int n=0;
try{
try{
n=24/n; //产生ArithmeticException异常
}catch(NumberFormatException e){
System.out.println("异常在内层捕获!");
}
}catch(ArithmeticException e){
System.out.println("Divided by zero!");
System.out.println("异常在外层被捕获!");
}
}
}思考:如何给本节例3中加上捕获异常?思考:如何给本节例3中加上捕获异常?public class ExceptionDemo3{
public static void main(String args[]){
String str="222.34 a 56 ";
Double i=new Double(str);
System.out.println(i);
}
}
二、将方法中的异常抛出:throw语句和throws子句二、将方法中的异常抛出:throw语句和throws子句第二种处理异常的方法:将方法中可能产生的异常抛出。
<方法名>(<参数行>)[throws<异常类1>,<异常类2>…]
{if (异常条件1成立)
throw new 异常类1();
if (异常条件2成立)
throw new 异常类2();
…
} null(1)声明异常:throws语句
一个方法不处理它产生的异常,而是沿着调用层次向上传递,由调用它的方法来处理这些异常,叫声明异常.
声明异常的方法
在产生异常的方法名后面加上要抛出(throws)的异常的列
void compute(int x)throws ArithmeticException {…}
例7:异常的抛出 例:exception\ThrowsDemo.java
null例:说出程序执行结果
public class exception1{
static void Proc(int sel) throws ArithmeticException,ArrayIndexOutOfBoundsException
{ System.out.println(“In Situation" + sel );
if (sel==0) {
System.out.println("no Exception caught");
return;
}else if(sel==1) {int iArray[]=new int[4];
iArray[10]=3;
}
} public static void main(String args[])
{ try { Proc(0);
Proc(1);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Catch"+e);
}
} null(2)抛出异常:throw语句
不是出错产生,而是人为地抛出;
然后在包含它的所有try块中从内向外寻找与其匹配的catch语句块。
throw ThrowableObject;
throw new ArithmeticException();
nullclass JavaThrow{
public static void main(String args[]){try{
throw new ArithmeticException();
}catch(ArithmeticException ae)
{ System.out.println(ae); }try{
throw new ArrayIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException ai){
System.out.println(ai);
}try {
throw new StringIndexOutOfBoundsException();
}catch(StringIndexOutOfBoundsException si){
System.out.println(si);
}}}null例8:异常的抛出
public class ThrowDemo{
public static void main(String []args){
int i;
try{
for(i=1;i<10;i++){
System.out.println(i);
if(i==1)throw new NumberFormatException("Throw Exception1");
if(i==2)throw new ArithmeticException("Throw Exception2");
if(i==3)break;
}
}catch(NumberFormatException e){
System.out.println("捕捉到 NumberFormatException");
}catch(ArithmeticException e){
System.out.println("捕捉到 ArithmeticException ");
}finally{
System.out.println("finally块被执行!");
}
}
}思考: 如何判断捕获的是哪类异常? exception\TestNumFormatException思考: 如何判断捕获的是哪类异常? exception\TestNumFormatException try{
String s =“wer”;
int k = Integer.parseInt(s);
}catch(__________________ e){
System.out.println(e.getMessage());
}finally{
System.out.println(“进入finally 处理模块");
}
NumberFormatException public static int parseInt(String s) throws NumberFormatException Exception 三、创建自己的异常 三、创建自己的异常 内置异常不可能始终足以捕获所有错误,因此需要用户自定义的异常类
用户自定义的异常类应为 Exception 类(或者Exception 类的子类)的子类
创建的任何用户自定义的异常类都可以获得 Throwable类定义的方法class ArraySizeException extends NegativeArraySizeException{
ArraySizeException() {
super(“您传递的是非法的数组大小”);
}
}该类是Exception的子类null//自定义异常 例:exception\
class MyException extends Exception{
private String str;
MyException(String s){
str=s;
}
String getstr(){ return str; }
void setstr(String s){ str=s; }
}
public class MyExceptionDemo{
public static void main(String []args){
try{
throw new MyException("自定义的异常:MyException !");
}catch(MyException e){
System.out.println("e.getstr(): "+e.getstr());
System.out.println("e.toString(): "+e.toString());
}
} }小结小结1.一般格式:正常程序和出错处理分离开来
try { Java statement;
}catch(ExceptionType1 ExceptionObject) {
Exception1 handling;
} catch(ExceptionType2 ExceptionObject) {
Exception2 handling;
}….
}finally {
final handling;
// (统一的出口,最终必定要执行)
}}小结小结2.把异常传播给堆栈,沿着被调用的顺序往前寻找,只要找到符合该异常种类彻底异常处理程序,就交给这部分程序去处理小结小结3.异常可以人为地抛出,用throw new 语句
4.异常可以是系统已经定义好的,也可以是用户自己定义的
5.用户自己定义的异常一定继承自Throwable或Exception类作业作业1、预习线程
2、哪些异常是运行时异常?
3、课后练习p206- 1,5