Java异常处理



 Java异常处理

 1:概念:

异常是java程序中运行时出现的错误的一种机制。   

抛出异常是指程序中如果出现异常,则抛出实例, 通过实例封装了异常的信息提交到Java运行时系统,这个过程叫做抛出异常。

       Exception这个术语是对词组“exceptional event”简短表达,其定义如下:

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.

 

       当在一个方法内部发生了一个错误,这个方法就创建一个对象并把它发送给运行系统,然后离开它。这个对象就是exception object,包含了有关错误的相关信息(错误发生时的程序状态及错误的类型)。创建一个exception对象并向运行系统发送,被称为“throwing an exception”。

 

       当一个方法抛出异常后,运行系统便试着查找原因并处理它。The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack (see the next figure).

2.异常体系结构

  

3:Java异常的关键字

  try :标示程序将要发生的异常语句块

  catch:捕获异常,先抛小异常,在抛出大异常。

  finally 不管try语句块中是否抛出异常都要执行finally块的语句,此关键字的好处是:如果打开数据库链接程序中断,可以在此处关闭链接,例如:打开文件,IO流文件

 throw 在方法中抛出异常指向一个异常方法

 throws 抛出方法异常。

 注意:声明方法异常时则需要在重写方法时,重写的方法和原方法保持一致或者不抛出方法异常。

4:语法结构

  

	try
	{
		//程序语句块
		System.out.println("开始执行异常...");
		System.out.println("程序运行结果:"+10/0);
		System.out.println("结束执行异常...");
	}
	catch(ArithmeticException e)
	{
		e.printStackTrace();
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
	}

 

	Connection conn =null;
	try
	{
		
		conn  = DriverManager.getConnection("","","");
		//程序语句块
		System.out.println("开始执行异常...");
		System.out.println("程序运行结果:"+10/0);
		System.out.println("结束执行异常...");
	}
	catch(ArithmeticException e)
	{
		e.printStackTrace();
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
	}
	finally 
	{
		try
		{
			if(conn!=null)
			{
				conn.close();
				conn=null;
			}
			
		}
		catch(Exception io)
		{
			io.printStackTrace();
		}
	}

 

  例子:

package com.ith.study;

import java.sql.Connection;
import java.sql.DriverManager;

 
@SuppressWarnings("serial")
public class DefaultException extends Exception {
 
 
	public DefaultException() {
		super();	//调用父类构造方法
	}
	
	public DefaultException(final String msg) {
		//super(msg);
		System.out.println(msg+"============");
	}
}



 

package com.ith.study;

import com.ith.study.DefaultException;

public class ThrowsException {

	/**
	 * @param args
	 * @throws DefaultException 
	 */
	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		ThrowsException throwtest=new ThrowsException();
		throwtest.throwsTestException();
	}
	
	public void throwsTestException() {
		
		System.out.println("===================");
		
		int i= 7/2;
		
		System.out.println("7/2======"+i);
		
		if(i>0) {
			try {
				throw new DefaultException("7/2抛出自定义异常");
			} catch (DefaultException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
	}

}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值