黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Java異常處理

系統(tǒng) 2351 0

 
 
  Java異常處理
 

  1:概念:  

 異常是java程序中運(yùn)行時(shí)出現(xiàn)的錯(cuò)誤的一種機(jī)制。       

 拋出異常是指程序中如果出現(xiàn)異常,則拋出實(shí)例, 通過實(shí)例封裝了異常的信息提交到Java運(yùn)行時(shí)系統(tǒng),這個(gè)過程叫做拋出異常。  

         Exception    這個(gè)術(shù)語(yǔ)是對(duì)詞組“          exceptional event    ”簡(jiǎn)短表達(dá),其定義如下:        

 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.        

 

        當(dāng)在一個(gè)方法內(nèi)部發(fā)生了一個(gè)錯(cuò)誤,這個(gè)方法就創(chuàng)建一個(gè)對(duì)象并把它發(fā)送給運(yùn)行系統(tǒng),然后離開它。這個(gè)對(duì)象就是          exception object    ,包含了有關(guān)錯(cuò)誤的相關(guān)信息(錯(cuò)誤發(fā)生時(shí)的程序狀態(tài)及錯(cuò)誤的類型)。創(chuàng)建一個(gè)          exception    對(duì)象并向運(yùn)行系統(tǒng)發(fā)送,被稱為“          throwing an exception              ”。        

 

        當(dāng)一個(gè)方法拋出異常后,運(yùn)行系統(tǒng)便試著查找原因并處理它。          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.異常體系結(jié)構(gòu)        

     張軍博客    

 3:Java異常的關(guān)鍵字        

   try :標(biāo)示程序?qū)⒁l(fā)生的異常語(yǔ)句塊        

   catch:捕獲異常,先拋小異常,在拋出大異常。        

   finally 不管try語(yǔ)句塊中是否拋出異常都要執(zhí)行finally塊的語(yǔ)句,此關(guān)鍵字的好處是:如果打開數(shù)據(jù)庫(kù)鏈接程序中斷,可以在此處關(guān)閉鏈接,例如:打開文件,IO流文件        

  throw 在方法中拋出異常指向一個(gè)異常方法        

  throws 拋出方法異常。        

  注意:聲明方法異常時(shí)則需要在重寫方法時(shí),重寫的方法和原方法保持一致或者不拋出方法異常。        

 4:語(yǔ)法結(jié)構(gòu)        

           

	try
	{
		//程序語(yǔ)句塊
		System.out.println("開始執(zhí)行異常...");
		System.out.println("程序運(yùn)行結(jié)果:"+10/0);
		System.out.println("結(jié)束執(zhí)行異常...");
	}
	catch(ArithmeticException e)
	{
		e.printStackTrace();
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
	}

 

 

	Connection conn =null;
	try
	{
		
		conn  = DriverManager.getConnection("","","");
		//程序語(yǔ)句塊
		System.out.println("開始執(zhí)行異常...");
		System.out.println("程序運(yùn)行結(jié)果:"+10/0);
		System.out.println("結(jié)束執(zhí)行異常...");
	}
	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();	//調(diào)用父類構(gòu)造方法
	}
	
	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();
			} 
		}
		
	}

}

 

 

 

 

 



 Java異常處理  


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論