【文章內(nèi)容簡(jiǎn)介】
blic class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (greetings [i])。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } greetings[0]=Hello world!“ greetings[1]=No, I mean it ! greetings[2]=HELLO WORLD!! i = 3 時(shí):繼續(xù)循環(huán) …… Always printed 時(shí) :拋出 ArrayIndexOutOfBoundsException類異常對(duì)象 時(shí) 執(zhí)行 catch ( ArrayIndexOutOfBoundsException e ) 時(shí): i = 1 時(shí):然后執(zhí)行 finally語句 時(shí):然后 i++ ……Resetting Index Va lue ……Always printed 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (greetings [i])。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } greetings[0]=Hello world!“ greetings[1]=No, I mean it ! greetings[2]=HELLO WORLD!! i = 0 時(shí):繼續(xù)循環(huán) (陷入死循環(huán) ) 3 時(shí):結(jié)束本次循環(huán)時(shí) i = 4 …… Always printed Resetting Index Va lue Always printed …… …… 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { i = 5 / 0。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } 此時(shí)捕獲的是一個(gè)算術(shù)異常 ArithmeticException對(duì)象 catch( Exception e )將捕獲所有異常的超類對(duì)象 ()將返回對(duì)象 e的內(nèi)容字符串 (Object類中方法 ) 輸出為: :/by zero 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (greetings [i])。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } 此時(shí)捕獲的是 ArrayIndexOutOfBoundsException對(duì)象 將被 catch(ArrayIndexOutOfBoundsException e ) 捕獲 因此不會(huì)再被 catch( Exception e ) 捕獲 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (greetings [i]+5/0)。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } i=0時(shí),捕獲的是 ArithmeticException類異常對(duì)象 :/by zero Always printed i=1和 2 :捕獲的都是 ArithmeticException類異常對(duì)象 i=3:捕獲 ArrayIndexOutOfBoundsException類異常對(duì)象 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (greetings [i]+5/0)。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } :/by zero Always printed i=3:捕獲 ArrayIndexOutOfBoundsException類異常對(duì)象 …… Always printed Resetting Index Value Resetting Index Value 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (5/0+greetings [i])。 } catch ( ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。 i = 1。 } catch( Exception e ) { ( () )。 } finally { (Always printed)。 } i ++。 } } } 2022年 2月 11日星期五 NCEPU 2 異常處理 二、異常的捕獲和處理 public class HelloWorld { public static void main (String args[]){ int i = 0。 String greetings [] = {Hello world!, No, I mean it !,HELLO WORLD!!}。 while ( i 4 ) { try { (5/0+greetings [i])。 } catch (Exception e ) { (() )。 i = 1。 } catch(ArrayIndexOutOfBoundsException e ) { (Resetting Index Value)。} finally { (Always printed)。 } i ++。 } } } catch 語句的排列順序是從特殊到一般 exception Exception has already been caught ch (Exception e ) 放在捕捉其他具體異常類的語句后 2022年 2月 11日星期五 NCEPU 練習(xí) ?設(shè)下列 trycatch語句塊中的第二個(gè)語句 s2將引起一個(gè)異常,試回答下列問題: try { s1。 s2。 s3。 } catch (ExceptionType e1) { dosomething。 } catch (ExceptionType e2) { dosomething。 } s4。 ( 1) S3會(huì)執(zhí)行么? ( 2)如果異常未被捕獲, s4會(huì)被執(zhí)行么? ( 3)如果 catch子句捕獲了異常, s4會(huì)執(zhí)行么? 2022年 2月 11日星期五 NCEPU 捕獲和處理異常 ? ExceptionType1,ExceptionType2,...,ExceptionTypek是產(chǎn)生的異常類型。 ?根據(jù)發(fā)生異常所屬的類,找到對(duì)應(yīng)的 catch語句,然后執(zhí)行其后的語句序列。 ?不論是否捕獲到,總要執(zhí)行 finally后面的語句。 ?在有多個(gè) 異常 需要捕獲時(shí), 異常 類型的順序很重要,在類層次樹中,一般的 異常 類型要放在后面,特殊的放在前面。 ?在 catch塊的后面可以放 finally 塊,其作用通常用于釋放資源 2022年 2月 11日星期五 異常類 (Exception)的常用方法 類的全名 + : + Message throw new Exception(“下標(biāo)越界” ) 2022年 2月 11日星期五 NCEPU 例: public class CS { public static void main(String args[]){ try{ (第一句 )。 (“第二句 5/0=+(5/0))。 (第三句 )。 } catch(Arithmetic