異常和錯(cuò)誤的區(qū)別和聯(lián)系
在Java中你踩,異常和錯(cuò)誤同屬于一個(gè)類:Throwable悦昵。異常和錯(cuò)誤都是Java異常處理重要的類懦趋,且各自都包含大量子類拣技。
Exception(異常)是應(yīng)用程序中出現(xiàn)的可預(yù)測,可恢復(fù)的問題娱据,通常是輕度或中度的問題蚪黑。異常通常是在特定環(huán)境下產(chǎn)生的,通常產(chǎn)生在特定的方法和操作中。例如程序使用readline方法時(shí)就可能產(chǎn)生IOException異常忌穿。
Exception 類有一個(gè)重要的子類 RuntimeException抒寂。RuntimeException 類及其子類表示“JVM 常用操作”引發(fā)的錯(cuò)誤。例如掠剑,若試圖使用空值對象引用屈芜、除數(shù)為零或數(shù)組越界,則分別引發(fā)運(yùn)行時(shí)異常(NullPointerException朴译、ArithmeticException)和 ArrayIndexOutOfBoundException井佑。
Error(錯(cuò)誤)大多表示運(yùn)行應(yīng)用程序時(shí)比較嚴(yán)重的錯(cuò)誤。大多錯(cuò)誤與編程者編寫的程序無關(guān)动分,可能是代碼運(yùn)營時(shí)jvm出現(xiàn)的問題。例如红选,當(dāng) JVM 不再有繼續(xù)執(zhí)行操作所需的內(nèi)存資源時(shí)澜公,將出現(xiàn) OutOfMemoryError。
異常的處理方式
異常的處理方式有兩種一種是處理異常喇肋,一種是聲明異常坟乾。
處理異常的三個(gè)部件:try,catch,finally
try塊:放在try塊的語句可能發(fā)生異常,編譯器知道可能發(fā)生異常所以用一個(gè)特殊結(jié)構(gòu)來處理這一塊大的語句蝶防。
catch塊:catch塊是try塊產(chǎn)生異常的接收者甚侣,一旦產(chǎn)生異常,try塊的運(yùn)行中止间学。
finally塊:無論try塊的運(yùn)行結(jié)果如何殷费,finally塊的代碼一定運(yùn)行。所以在需要關(guān)閉某些資源時(shí)可以將關(guān)閉資源的代碼放在finally塊中用來清理代碼低葫。
try-catch-finally規(guī)則:
- try 塊后可同時(shí)接 catch 和 finally 塊详羡,但至少有一個(gè)塊。
- 必須遵循塊順序:若代碼同時(shí)使用 catch 和 finally 塊嘿悬,則必須將 catch 塊放在 try 塊之后实柠。
- 一個(gè) try 塊可能有多個(gè) catch 塊。有多個(gè)catch塊時(shí)執(zhí)行第一個(gè)匹配塊善涨。
下面的例子中使用了try窒盐,catch,finally:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExceptionHandleExamples {
public static void main (String[] args){
System.out.println("Enter input text to console:");
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader inputReader = new BufferedReader(inputStreamReader);
try{
String inputText = inputReader.readLine();
System.out.println("Read: "+inputText);
}catch(IOException exc){
System.out.println("Exception encountered: "+ exc );
}finally{
System.out.println("End.");
}
}
}
try塊中readLine()可能產(chǎn)生IOException異常钢拧,try塊可以檢測這部分代碼是否會產(chǎn)生異常蟹漓,如果產(chǎn)生異常則交給catch塊處理。最后不論try塊的運(yùn)行結(jié)果是什么源内,finally塊都會運(yùn)行輸出"End. "牧牢。
** 異常聲明 **
以下示例如果數(shù)組的索引大于數(shù)組的長度則拋出數(shù)組越界異常。
public int getNumberFromArray(int[] array, int index) throws ArrayIndexOutOfBoundsException {
if (index > array.length) {
throw new ArrayIndexOutOfBoundsException("數(shù)組越界異常");
}
return array[index];
}
幾種常見異常的處理
算術(shù)異常類:ArithmeticException
public static void main (String[] args){
int a = 1;
int b = 0;
try{
int c = a/b;
}catch(ArithmeticException e){
System.out.println("發(fā)生了算術(shù)異常。");
}
}
數(shù)組越界異常:ArrayIndexOutOfBoundsException
public static void main (String[] args){
int [] array = {1,2,12};
try{
System.out.println(array[5]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("發(fā)生了數(shù)組越界異常塔鳍。");
}
}
類型強(qiáng)制轉(zhuǎn)換異常:ClassCastException
try{
Object x = new String("String");
System.out.println((Integer) x);
}catch(ClassCastException e){
System.out.println("類型強(qiáng)制轉(zhuǎn)換異常");
}
數(shù)字格式異常 :NumberFormatException
try{
System.out.println(Integer.parseInt("it"));
}catch(NumberFormatException e){
System.out.println("數(shù)字格式異常");
}
輸入輸出異常:IOException
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExceptionHandleExamples {
public static void main (String[] args){
System.out.println("Enter input text to console:");
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader inputReader = new BufferedReader(inputStreamReader);
try{
String inputText = inputReader.readLine();
System.out.println("Read: "+inputText);
}catch(IOException exc){
System.out.println("Exception encountered: "+ exc );
}finally{
System.out.println("End.");
}
}
}