最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

java常見異常機(jī)制

2022-07-08 17:56 作者:虛云幻仙  | 我要投稿

/**
* 異常機(jī)制
*/

public class Test01 {
? ?public static void main(String[] args) {
?? ? ? ?int a = 1/0;
? ? ? ?/*
? ? ? ?Exception in thread "main" java.lang.ArithmeticException: / by zero
? ? ? at Test01.main(Test01.java:3)
? ? ? 異常 線程"main" ?java.lang包中的ArithmeticException算數(shù)異常類 ?異常描述:by 0
? ? ? 在 類Test01的main() Test01.java文件第3行

? ? ? main方法會(huì)創(chuàng)建一個(gè)主線程
? ? ? Process finished with exit code 1
? ? ? 進(jìn)程已完成 隨著退出代碼1 代碼0是正常退出 代碼1是異常退出
? ? ? Exception異常類 出現(xiàn)異常時(shí)會(huì)生成對(duì)應(yīng)異常類的對(duì)象/拋出異常 并且中止程序
? ? ? 拋出異常由系統(tǒng)創(chuàng)建
? ? ? ? */

? ? ? ?try{
? ? ? ? ? ?int a = 1/0;
? ? ? ?}catch (Exception e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?int a = 0;
? ? ? ?System.out.println(a);
? ? ? ?/*
? ? ? ?java.lang.ArithmeticException: / by zero
? ? ? at Test01.main(Test01.java:15)
? ? ? ?0
? ? ? ?Process finished with exit code 0

? ? ? ?try嘗試{}語句塊 當(dāng)出現(xiàn)異常catch抓住異常 (e)用引用變量e指向該異常對(duì)象 執(zhí)行{}語句塊 語句塊內(nèi)默認(rèn)調(diào)用異常對(duì)象e.printStackTrace()打印stack棧trace跟蹤信息
? ? ? ?如果沒有異常發(fā)生會(huì)正常執(zhí)行try內(nèi)的語句 出現(xiàn)異常會(huì)生成異常對(duì)象交給catch并執(zhí)行catch內(nèi)的語句
? ? ? ?之后繼續(xù)往后執(zhí)行程序
? ? ? ?第一個(gè)a在try語句塊內(nèi)聲明 出了語句塊失效 需要重新聲明
? ? ? ?將錯(cuò)誤代碼包在try catch中 運(yùn)行時(shí)遇到異常也會(huì)繼續(xù)完成try catch后面的內(nèi)容 進(jìn)程正常退出exit code 0
? ? ? ? */

? ? ? ?int b=0;
? ? ? ?if (b!=0){
? ? ? ? ? ?System.out.println(1/b);
? ? ? ?}
? ? ? ?//通過if判斷來規(guī)避異常 使程序能繼續(xù)往下執(zhí)行

? ? ? ?String str = "1";
? ? ? ?System.out.println(str.charAt(1));
? ? ? ?/*
? ? ? ?charAt(index)方法的內(nèi)容如下
? ? ? ? ? ?public char charAt(int index) {
? ? ? ? ? ? ? ?if ((index < 0) || (index >= value.length)) {
? ? ? ? ? ? ? ? ? ?throw new StringIndexOutOfBoundsException(index);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?return value[index];
? ? ? ? ? ?}
? ? ? ? ? ?返回值為char類型 判斷索引index<0或者索引超過/等于字符串的數(shù)組的長(zhǎng)度 throw拋出 new Exception構(gòu)造器new一個(gè)對(duì)象
? ? ? ? ? ?if(false)返回index位的值

? ? ? ? ? ?Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
? ? ? ? ? at java.lang.String.charAt(String.java:658)
? ? ? ? ? at Test01.main(Test01.java:42)
? ? ? ? ? 異常 在線程"main"中 異常對(duì)象是StringIndexOfBoundsException類 對(duì)應(yīng)charAt()方法內(nèi)的throw new xxxx ?index out of超過 range范圍 :1索引傳的參數(shù)是1 即str.charAt(1)
? ? ? ? ? 在 java.lang包String類charAt方法(String.java文件 :658行) 658行對(duì)應(yīng)throw new StringIndexOutOfBoundsException(index);
? ? ? ? ? 在 Test01.main方法(Test01.java文件:42行)
? ? ? ? */


? ? ? ?Object o =null;
? ? ? ?System.out.println(o.toString());
? ? ? ?/*
? ? ? ? ? ?Exception in thread "main" java.lang.NullPointerException
? ? ? ? ? at Test01.main(Test01.java:63)
? ? ? ? ? 異常對(duì)象為 java.lang.NullPointerException類 null空pointer指針 引用變量o為null 找不到o的.toString()方法
? ? ? ? */

? ?}
}

class Equipment{ }
class Computer extends Equipment{}
class Television extends Equipment{}
class cast{
? ?public static void main(String[] args) {
? ? ? ?Equipment c = new Computer();
? ? ? ?Television t = (Television)c;
? ? ? ?/*
? ? ? ?Exception in thread "main" java.lang.ClassCastException: Computer cannot be cast to Television
? ? ? at cast.main(Test01.java:78)
? ? ? ? ? ?class類cast強(qiáng)制轉(zhuǎn)換exception異常 Computer不能強(qiáng)制轉(zhuǎn)換為Television
? ? ? ? */

? ? ? ?if (c instanceof Television){
? ? ? ? ? ?Television te = (Television) c;
? ? ? ?}
? ? ? ?//添加判斷 c是否Television的實(shí)例

? ? ? ?int[] array = new int[5];
? ? ? ?System.out.println(array[5]);
? ? ? ?/*
? ? ? ?Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
? ? ? at cast.main(Test01.java:90)
? ? ? ArrayIndexOutOf數(shù)組索引超過Bounds界限Exception: 5 這里的5是array[5]的實(shí)參5 ?:冒號(hào)后面為異常的描述
? ? ? ? */


? ? ? ?File f = new File("C:\\CheckedException.txt");
? ? ? ?// "\t\n\r\'\"\\ " 斜杠加字符 escape character轉(zhuǎn)義字符 C:\CheckedException.txt 改成C:/
? ? ? ? f.createNewFile();
? ? ? ?/*
? ? ? ?Unhandled exception:java.io.IOException 在編譯階段產(chǎn)生的異常 checked exception 還沒到run階段就會(huì)報(bào)異常
? ? ? ?未處理異常 創(chuàng)建文件就會(huì)報(bào)異常 需要包裹try catch來使用
? ? ? ? */

? ? ? ?try {
? ? ? ? ? ?f.createNewFile();
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?//拋出異常時(shí)catch io異常
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ? ?//拋出一個(gè)新的運(yùn)行時(shí)異常
? ? ? ?}
? ?}
}

java常見異常機(jī)制的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
江川县| 宜昌市| 二连浩特市| 新巴尔虎右旗| 清苑县| 随州市| 长武县| 广西| 张家口市| 衢州市| 集贤县| 大丰市| 大荔县| 嘉兴市| 乐都县| 秦皇岛市| 清水河县| 安庆市| 株洲县| 互助| 孟村| 通山县| 和顺县| 农安县| 富川| 花垣县| 利川市| 德昌县| 固始县| 雅江县| 兴山县| 增城市| 株洲市| 宁乡县| 卓尼县| 宁远县| 马公市| 阿合奇县| 商都县| 应城市| 辛集市|