Java基礎(chǔ)之異常處理操作示例
本文實例講述了Java基礎(chǔ)之異常處理操作。分享給大家供大家參考,具體如下:
示例代碼:
public class ExecDemo {
public static void main(String[] args) {
int[] nums = new int[4];
System.out.println("before the exception:");
try { //try代碼塊 try catch代碼塊可以嵌套
try{
nums[7] = 10; //數(shù)組越界
System.out.println("no exception:");
}catch(ArithmeticException e){
e.printStackTrace();
}
}catch(ArithmeticException e) { //catch代碼塊 多個catch塊
e.printStackTrace(); //打印異常信息
}catch(ArrayIndexOutOfBoundsException e){ // 捕獲數(shù)組越界錯誤 捕獲子類異常
e.printStackTrace();
}catch(Throwable e){ //捕獲超類異常 Throwable是所有異常的超類
e.printStackTrace();
}
System.out.println("after the exception");
}
}
拋出異常:
public class ThrowDemo {
public static void main(String[] args) {
try {
System.out.println("before throw:");
throw new ArithmeticException(); //throw關(guān)鍵字拋出一個ArithmeticException()異常(手動拋出異常)
} catch (ArithmeticException e) { //捕獲異常
System.out.println("exception caught:");
}
System.out.println("after try{}catch{}:");
}
}
重新拋出異常:
class Rethrow {
public static void genException() {
int[] numer = {2,4,6,8,10,12};
int[] demon = {2,0,3,4,0};
for(int i=0;i < numer.length;i++){
try { //try代碼塊
System.out.println(numer[i]/demon[i]);
} catch (ArithmeticException e) { //多個catch()塊
System.out.println("can't dev by zero:");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("no error:");
throw e; //throw 重寫拋出異常
}
}
}
}
public class RethrowDemo{
public static void main(String args[]) {
try {
Rethrow.genException();
} catch (ArrayIndexOutOfBoundsException e) { //捕獲重新拋出的異常
System.out.println("error error error error error:");
}
finally{ //finally代碼塊在try catch執(zhí)行完時執(zhí)行的。
System.out.println("Leaving try.");
}
}
}
throws語句:一個方法產(chǎn)生自己不做處理的異常,用throws拋出到外層(誰調(diào)用,誰處理異常)
public class ThrowsDemo {
public static char prompt(String str) throws java.io.IOException{//prompt()方法產(chǎn)生自己不做處理的IOException異常,拋出到外層,誰調(diào)用誰處理異常
System.out.print(str +":");
return (char) System.in.read();
}
public static void main(String[] args) {
char ch;
try {
ch = prompt("enter a letter"); //prompt()可能拋出異常,
} catch (java.io.IOException e) { //捕獲prompt()拋出的異常
System.out.println("IOException occurred");
ch = 'x';
}
System.out.println("you pressed:"+ ch);
}
}
可以用一個catch()捕獲多個異常:
try{
}
catch(ArithmeticException|ArrayIndexOutOfBoundsException e){//同時捕獲多個異常
}
自定義異常:
class NonIntResultException extends Exception{ //自定義異常繼承子Exception
int n ,d;
NonIntResultException(int i,int j){
n = i;
d = j;
}
public String toString() {
return "result of "+ n +"/"+ d +" is non-integer.";
}
}
public class CustomExceptionDemo {
public static void main(String[] args) {
int numer[] = {4,8,15,32,64,127,256,512};
int denom[] = {2,0,4,4,0,8};
for(int i=0;i<numer.length;i++) {
try {
if((numer[i]%2)!=0) {
throw new NonIntResultException(numer[i],denom[i]); //拋出自定義異常
}
System.out.println(numer[i] +"/"+ denom[i] +" is "+ numer[i]/denom[i]);
} catch (ArithmeticException e) { //捕獲ArithmeticException異常
System.out.println("can't divide by zero!");
}catch (ArrayIndexOutOfBoundsException e) { //捕獲ArrayIndexOutOfBoundsException 異常
System.out.println("no matching element found.");
}catch (NonIntResultException e) { //捕獲自定義異常
System.out.println(e);
}
}
}
}
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O計入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關(guān)文章
SpringBoot實現(xiàn)JPA多數(shù)據(jù)源配置小結(jié)
本文主要介紹了SpringBoot實現(xiàn)JPA多數(shù)據(jù)源配置小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-01-01
WebUploader+SpringMVC實現(xiàn)文件上傳功能
WebUploader是由Baidu團隊開發(fā)的一個簡單的以HTML5為主,F(xiàn)LASH為輔的現(xiàn)代文件上傳組件。這篇文章主要介紹了WebUploader+SpringMVC實現(xiàn)文件上傳功能,需要的朋友可以參考下2017-06-06
Java Idea TranslationPlugin翻譯插件使用解析
這篇文章主要介紹了Java Idea TranslationPlugin翻譯插件使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
java中instanceof和getClass()的區(qū)別分析
本篇文章介紹了,在java中instanceof和getClass()的區(qū)別分析。需要的朋友參考下2013-04-04
Java微信公眾平臺開發(fā)(10) 微信自定義菜單的創(chuàng)建實現(xiàn)
這篇文章主要為大家詳細介紹了Java微信公眾平臺開發(fā)第十步,微信自定義菜單的創(chuàng)建實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

