c java 异常处理,Java 异常处理

异常就是指程序执行时发生的问题。

异常发生的原因有很多,通常包含以下几大类:

用户输入了非法数据。

要打开的文件不存在。

网络通信时连接中断,或者JVM内存溢出。

这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。-

要理解Java异常处理是如何工作的,你需要掌握以下三种类型的异常:

检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略。

运行时异常: 运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。

错误: 错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的。

Exception类的层次:

所有的异常类是从java.lang.Exception类继承的子类。

Exception类是Throwable类的子类。除了Exception类外,Throwable还有一个子类Error 。

Java程序通常不捕获错误。错误一般发生在严重故障时,它们在Java程序处理的范畴之外。

Error用来指示运行时环境发生的错误。

例如,JVM内存溢出。一般地,程序不会从错误中恢复。

异常类有两个主要的子类:IOException类和RuntimeException类。

12-130Q1234I6223.jpg

在Java 内置类中,有大部分常用检查性和非检查性异常。

异常方法:

下面的列表是Throwable 类的主要方法:

序号

方法及说明

1

public String getMessage()

返回关于发生的异常的详细信息。这个消息在Throwable 类的构造函数中初始化了。

2

public Throwable getCause()

返回一个Throwable 对象代表异常原因。

3

public String toString()

使用getMessage()的结果返回类的串级名字。

4

public void printStackTrace()

打印toString()结果和栈层次到System.err,即错误输出流。

5

public StackTraceElement [] getStackTrace()

返回一个包含堆栈层次的数组。下标为0的元素代表栈顶,最后一个元素代表方法调用堆栈的栈底。

6

public Throwable fillInStackTrace()

用当前的调用栈层次填充Throwable 对象栈层次,添加到栈层次任何先前信息中。

捕获异常:

使用try和catch关键字可以捕获异常。try/catch代码块放在异常可能发生的地方。

try/catch代码块中的代码称为保护代码,使用 try/catch的语法如下:

try

{

//Protected code

}catch(ExceptionName e1)

{

//Catch block

}

Catch语句包含要捕获异常类型的声明。当保护代码块中发生一个异常时,try后面的catch块就会被检查。

如果发生的异常包含在catch块中,异常会被传递到该catch块,这和传递一个参数到方法是一样。

示例:

下面的例子中声明有两个元素的一个数组,当代码试图访问数组的第三个元素的时候就会抛出一个异常。

// File Name : ExcepTest.java

import java.io.*;

public class ExcepTest{

public static void main(String args[]){

try{

int a[] = new int[2];

System.out.println("Access element three :" + a[3]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("Exception thrown :" + e);

}

System.out.println("Out of the block");

}

}

运行结果如下:

Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3

Out of the block

多重捕获块:

一个try代码块后面跟随多个catch代码块的情况就叫多重捕获。

多重捕获块的语法如下所示:

try

{

//Protected code

}catch(ExceptionType1 e1)

{

//Catch block

}catch(ExceptionType2 e2)

{

//Catch block

}catch(ExceptionType3 e3)

{

//Catch block

}

上面的代码段包含了3个catch块。

可以在ry语句后面添加任意数量的catch块。

如果保护代码中发生异常,异常被抛给第一个catch块。

如果抛出异常的数据类型与ExceptionType1匹配,它在这里就会被捕获。

如果不匹配,它会被传递给第二个catch块。

如此,直到异常被捕获或者通过所有的catch块。

示例:

该实例展示了怎么使用多重try/catch。

try

{

file = new FileInputStream(fileName);

x = (byte) file.read();

}catch(IOException i)

{

i.printStackTrace();

return -1;

}catch(FileNotFoundException f) //Not valid!

{

f.printStackTrace();

return -1;

}

throws/throw关键字:

如果一个方法没有捕获一个检查性异常,那么该方法必须使用throws 关键字来声明。throws关键字放在方法签名的尾部。

也可以使用throw关键字抛出一个异常,无论它是新实例化的还是刚捕获到的。

下面方法的声明抛出一个RemoteException异常:

import java.io.*;

public class className

{

public void deposit(double amount) throws RemoteException

{

// Method implementation

throw new RemoteException();

}

//Remainder of class definition

}

一个方法可以声明抛出多个异常,多个异常之间用逗号隔开。

例如,下面的方法声明抛出RemoteException和InsufficientFundsException:

import java.io.*;

public class className

{

public void withdraw(double amount) throws RemoteException,

InsufficientFundsException

{

// Method implementation

}

//Remainder of class definition

}

finally关键字

finally关键字用来创建在try代码块后面执行的代码块。

无论是否发生异常,finally代码块中的代码总会被执行。

在finally代码块中,可以运行清理类型等收尾善后性质的语句。

finally代码块出现在catch代码块最后,语法如下:

try

{

//Protected code

}catch(ExceptionType1 e1)

{

//Catch block

}catch(ExceptionType2 e2)

{

//Catch block

}catch(ExceptionType3 e3)

{

//Catch block

}finally

{

//The finally block always executes.

}

示例:

public class ExcepTest{

public static void main(String args[]){

int a[] = new int[2];

try{

System.out.println("Access element three :" + a[3]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("Exception thrown :" + e);

}

finally{

a[0] = 6;

System.out.println("First element value: " +a[0]);

System.out.println("The finally statement is executed");

}

}

}

运行结果如下:

Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3

First element value: 6

The finally statement is executed

注意下面事项:

catch不能独立于try存在。

在try/catch后面添加finally块并非强制性要求的。

try代码后不能既没catch块也没finally块。

try, catch, finally块之间不能添加任何代码。

声明自定义异常:

在Java中你可以自定义异常。编写自己的异常类时需要记住下面的几点。

所有异常都必须是Throwable的子类。

如果希望写一个检查性异常类,则需要继承Exception类。

如果你想写一个运行时异常类,那么需要继承RuntimeException 类。

可以像下面这样定义自己的异常类:

class MyException extends Exception{

}

只继承Exception 类来创建的异常类是检查性异常类。

下面的InsufficientFundsException类是用户定义的异常类,它继承自Exception。

一个异常类和其它任何类一样,包含有变量和方法。

示例:

// File Name InsufficientFundsException.java

import java.io.*;

public class InsufficientFundsException extends Exception

{

private double amount;

public InsufficientFundsException(double amount)

{

this.amount = amount;

}

public double getAmount()

{

return amount;

}

}

为了展示如何使用我们自定义的异常类,

在下面的CheckingAccount 类中包含一个withdraw()方法抛出一个InsufficientFundsException异常。

// File Name CheckingAccount.java

import java.io.*;

public class CheckingAccount

{

private double balance;

private int number;

public CheckingAccount(int number)

{

this.number = number;

}

public void deposit(double amount)

{

balance += amount;

}

public void withdraw(double amount) throws

InsufficientFundsException

{

if(amount <= balance)

{

balance -= amount;

}

else

{

double needs = amount - balance;

throw new InsufficientFundsException(needs);

}

}

public double getBalance()

{

return balance;

}

public int getNumber()

{

return number;

}

}

下面的BankDemo程序示范了如何调用CheckingAccount类的deposit() 和withdraw()方法。

// File Name BankDemo.java

public class BankDemo

{

public static void main(String [] args)

{

CheckingAccount c = new CheckingAccount(101);

System.out.println("Depositing $500...");

c.deposit(500.00);

try

{

System.out.println("

Withdrawing $100...");

c.withdraw(100.00);

System.out.println("

Withdrawing $600...");

c.withdraw(600.00);

}catch(InsufficientFundsException e)

{

System.out.println("Sorry, but you are short $"

+ e.getAmount());

e.printStackTrace();

}

}

}

编译上面三个文件,并运行程序BankDemo,得到结果如下所示:

Depositing $500...

Withdrawing $100...

Withdrawing $600...

Sorry, but you are short $200.0

InsufficientFundsException

at CheckingAccount.withdraw(CheckingAccount.java:25)

at BankDemo.main(BankDemo.java:13)

通用异常:

在Java中定义了两种类型的异常和错误。

JVM(Java虚拟机)异常:由JVM抛出的异常或错误。例如:NullPointerException类,ArrayIndexOutOfBoundsException类,ClassCastException类。

程序级异常:由程序或者API程序抛出的异常。例如IllegalArgumentException类,IllegalStateException类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值