异常处理
5.1 Java异常处理的基础知识
5.1.1 错误与异常
5.1.1 异常处理机制
1.抛出异常 2.捕获异常 3.异常处理的类层次
5.2 异常类的产生、捕获与处理
5.3 异常的分类
5.4 抛出异常
5.5 自定义异常类
5.2 异常类的产生、捕获与处理
1.异常的产生
2.使用 try-catch-finally语句捕获与处理异常
3,Exception异常类的方法
public class Try1
{
public static void main (String args[])
{
int i=0;
int a[] = {5,6,7,8};
for(i=0;i<5;i++)
System.out.println(" a["+i+"]="+a[i]);
System.out.print("3/0="+(3/0));
}
}
ArrayIndexOutOfBoundsException
D:\myjava>javac Try1.java
D:\myjava>java Try1
a[0]=5
a[1]=6
a[2]=7
a[3]=8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at Try1.main(Try1.java:8)
D:\myjava>javac Try1.java
D:\myjava>java Try1
Exception in thread "main" java.lang.ArithmeticException,/ by zero
at Try1.main(try1.java:9)
使用 try-catch-finally语句捕获与处理异常
try
{
<语句 1>
}
catch(ExceptionType e)
{
<语句 2>
}
finally
{
<语句 3>
}
public class Try2
{ public static void main (String args[])
{ int i=0;
int a[] = {5,6,7,8};
for(i=0;i<5;i++)
{ try
{ System.out.print("a["+i+"]/"+i+"="+(a[i]/i));
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.print("捕获数组下标越界异常 !");
}
catch(ArithmeticException e)
{System.out.print("捕获算术异常 !");
}
catch(Exception e)
{ System.out.print("捕获 "+e.getMessage()+"异常 !");
} //显示异常信息
finally
{ System.out.println(" finally i="+i);
}
}
System.out.println("继续 !");
}
}
捕获算术异常 ! finally i=0
a[1]/1=6 finally i=1
a[2]/2=3 finally i=2
a[3]/3=2 finally i=3
捕获数组下标越界异常 ! finally i=4
继续 !
public class Try2
{ public static void main (String args[])
{ int i=0;
int a[] = {5,6,7,8};
for(i=0;i<5;i++)
{ try
{ System.out.print("a["+i+"]/"+i+"="+(a[i]/i));
}
/*catch(ArrayIndexOutOfBoundsException e)
{ System.out.print("捕获数组下标越界异常 !");
}
catch(ArithmeticException e)
{ System.out.print("捕获算术异常 !");
}*/
catch(Exception e)
{ System.out.print("捕获 "+e.getMessage()+"异常 !");
} //显示异常信息
finally
{ System.out.println(" finally i="+i);
}
}
System.out.println("继续 !");
}
}
D:\myjava>java Try2
捕获 / by zero异常 ! finally i=0
a[1]/1=6 finally i=1
a[2]/2=3 finally i=2
a[3]/3=2 finally i=3
捕获 null异常 ! finally i=4
继续 !
5.3 异常的分类
1,异常类层次图程序对错误与异常的三种处理方式,
① 程序不能处理的错误
② 程序应避免而不捕获的异常
③ 程序必须捕获的异常例 5.3 文件没有找到异常类
2,常见的公用异常类
import java.io.*;
public class Try3
{
public static void main (String args[])
{
FileInputStream fis = new FileInputStream("autoexec.bat");
System.out.println("I can not found this file!");
}
}
D:\myjava>javac Try3.java
Try3.java:6,unreported exception java.io.FileNotFoundException; must be caught
or declared to be thrown
FileInputStream fis = new FileInputStream("autoexec.bat");
^
1 error
D:\myjava>
5.4 抛出异常
public class Try4
{
public void run(byte k) //求 k的阶乘
{
byte y=1,i;
for (i=1;i<=k;i++)
y = (byte)(y * i);
System.out.println(k+"!="+y);
}
public static void main (String args[])
{
Try4 a = new Try4();
for (byte i=1;i<10;i++)
a.run(i);
}
}
1!=1
2!=2
3!=6
4!=24
5!=120
6!=-48
7!=-80
8!=-128
9!=-128
1.使用 throw语句抛出异常
public class Try5
{
public void run(byte k)
{
byte y=1,i=1;
System.out.print(k+"!=");
for (i=1;i<=k;i++)
{
try
{
if(y>Byte.MAX_VALUE/i) //Integer类的常量
throw new Exception(“overflow”); //溢出时抛出异常,12行
else
y = (byte) (y * i);
}
catch(Exception e)
{
System.out.println("exception,"+e.getMessage());
e.printStackTrace(); //显示异常信息
System.exit(0);
}
}
System.out.println(y);
}
public static void main (String args[])
{
Try5 a = new Try5();
for (byte i=1;i<10;i++)
a.run(i); //29行
}
}
1!=1
2!=2
3!=6
4!=24
5!=120
6!=exception,overflow
java.lang.Exception,overflow
at Try5.run(Try5.java:12)
at Try5.main(Try5.java:29)
2.抛出异常的方法与调用方法处理异常
public class Try6
{
public void calc(byte k) throws Exception //抛出异常
{
byte y=1,i=1;
System.out.print(k+"!=");
for (i=1;i<=k;i++)
{
if(y>Byte.MAX_VALUE/i)
throw new Exception(“overflow”); //10行
else
y = (byte)(y * i);
}
System.out.println(y);
}
public void run(byte k) //捕获并处理异常
{ try
{ calc(k); //20行
}
catch(Exception e)
{
System.out.println("exception,"+e.getMessage());
e.printStackTrace();
System.exit(0);
}
}
public static void main (String args[])
{
Try6 a = new Try6();
for (byte i=1;i<10;i++)
a.run(i); //33行
}
}
Try6的运行结果
D:\myjava>java Try6
1!=1
2!=2
3!=6
4!=24
5!=120
6!=exception,overflow
java.lang.Exception,overflow
at Try6.calc(Try6.java:10)
at Try6.run(Try6.java:20)
at Try6.main(Try6.java:33)
3.由方法抛出异常交系统处理
import java.io.*;
public class Try3
{
public static void main (String args[]) throw IOException
{
FileInputStream fis = new FileInputStream(“autoexec.bat”);
}
}
5.5 自定义异常类
class OverflowException extends Exception //自定义异常类
{
public void printMsg()
{
System.out.println("exception,"+this.getMessage());
this.printStackTrace();
System.exit(0);
}
}
public class Try7
{
public void calc(byte k) throws OverflowException //抛出异常
{
byte y=1,i=1;
System.out.print(k+"!=");
for (i=1;i<=k;i++)
{
if(y>Byte.MAX_VALUE/i)
throw new OverflowException(); //19行
else
y = (byte)(y * i);
}
System.out.println(y);
}
public void run(byte k) //捕获并处理异常
{ try
{ calc(k); //29行
}
catch(OverflowException e)
{ e.printMsg();
}
}
public static void main (String args[])
{ Try7 a = new Try7();
for (byte i=1;i<10;i++)
a.run(i); //40行
}
}
Try7的运行结果
1!=1
2!=2
3!=6
4!=24
5!=120
6!=exception,null
OverflowException
at Try7.calc(Try7.java:19)
at Try7.run(Try7.java:29)
at Try7.main(Try7.java:40)