欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Java異常處理

系統(tǒng) 1987 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)論
主站蜘蛛池模板: 天堂网成人 | 五月色开心婷婷丁香在线 | www.尤物视频 | 成人毛片免费网站 | 成人av在线播放 | 欧美剧场成人精品午夜 | 成人精品视频 | 国产精彩视频在线 | 国产一区二区在线免费观看 | 深夜福利网站 | 尤物国产在线精品福利一区 | 99久久自偷自偷国产精品不卡 | 看片国产 | 亚洲看片| 午夜精品一区二区三区在线视 | 韩漫重考生漫画画免费读漫画下拉式土豪漫 | 千涩成人网 | 亚洲欧美精品伊人久久 | 免费人成网站线观看合集 | 男人与女人做爰毛片A片 | 夜夜爽99久久国产综合精品女不卡 | 日韩中文字幕一区 | 亚洲人成网站在线播放观看 | 亚洲综合区 | 自拍偷拍亚洲欧美 | 国产精品国产精品国产专区不卡 | 欧美日韩免费在线观看 | 黄色精品 | 欧美一区二区三区精品国产 | 奇米视频在线观看 | 亚洲一区美女 | 图片区乱熟图片区小说 | 中文字幕日韩在线 | 日韩福利视频 | 欧美日韩一区在线观看 | 亚洲精品久久久久久蜜臀 | 久久亚洲国产高清 | 丝袜美腿一区二区三区动态图 | 奇米影视色 | 亚洲欧美日韩中文字幕在线一区 | 黄色网址在线免费播放 |