Java 异常处理

1、Java的异常处理主要依靠try、catch、finally、Throw、Throws五个关 键字。Java异常分为Checked异常和Runtime异常。Checked异常是可以在编译阶段处理的异常,而Runtime异常无需处理。

2、异常处理

2.1 try-catch

当Java运行时环境受到异常的时候,会寻找能处理异常的catch块,若是找到则异常交给相应的catch块处理,这叫做捕获异常。若是找不到相应的异常,则运行时环境终止,Java程序就会退出推出。

public void readFile()
	{
		FileInputStream  fis = null; 
		byte[] b = new byte[1024];
		int hasRead=0;
		try {
			fis = new FileInputStream("E:"+File.separator+"20151202"+File.separator+"env.txt");
			while((hasRead=fis.read(b))!=-1)
			{
				System.out.println(new String(b,0,hasRead));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
2.2 Java异常的继承关系

Java所有的非正常情况分为Error和Exception,它们都继承之Throwable。Error错误一般是与虚拟机相关的错误如系统崩溃、动态链接失败。这种错误不能捕获也不能恢复。

finally块中使用return语句可能导致出现异常情况。

public void readFile()
	{
		FileInputStream  fis = null; 
		byte[] b = new byte[1024];
		int hasRead=0;
		try {
			fis = new FileInputStream("E:"+File.separator+"20151202"+File.separator+"env.txt");
			while((hasRead=fis.read(b))!=-1)
			{
				System.out.println(new String(b,0,hasRead));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(null!=fis)
			{
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("文件关闭!");
			}
			
		}
	}
	


2.3 Throws

它处理异常的思路是:当前方法不知道如何处理这种类型的异常时,该异常由上一级异常。若是main方法也不知道如何处理这种异常,也可以使用throws声明抛出异常,该异常将交给jvm处理。jvm处理异常的方法是,打印异常的跟踪栈信息,并终止程序运行。Throws异常可以抛出多个异常,使用逗号隔开。

	public void readFile() throws FileNotFoundException ,IOException
	{
		FileInputStream  fis = null; 
		byte[] b = new byte[1024];
		int hasRead=0;
			fis = new FileInputStream("E:"+File.separator+"20151202"+File.separator+"env.txt");
			while((hasRead=fis.read(b))!=-1)
			{
				System.out.println(new String(b,0,hasRead));
			}
	}
2.4 自定义异常

package SomeSystemClassLearning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SomeException {

	public void readFile()
	{
		FileInputStream  fis = null; 
		byte[] b = new byte[1024];
		int hasRead=0;
		try {
			fis = new FileInputStream("E:"+File.separator+"20151202"+File.separator+"env.txt");
			while((hasRead=fis.read(b))!=-1)
			{
				System.out.println(new String(b,0,hasRead));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			if(null!=fis)
			{
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("文件关闭!");
			}
			
		}

	}
	public void testException() throws MakeException
	{
		int a = 8;
		int b=0;
		double result = 0.0;
		try {
			result = a/b;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new MakeException("除数不能是整数!");
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   SomeException se =new SomeException();
//   se.readFile();
   try {
	se.testException();
} catch (MakeException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	System.err.print(e.getMessage());
}
   
	}

}

2.5 异常处理的规则

(1)程序代码混乱度最小;

(2)通知合适人员;

(3)使用合适的方法结束异常;

(4)不要过度使用异常。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值