異常:就是在程序的運行過程中所發生的不正常的事件,它會中斷正在運行的程序
當程序中有異常時就需要進行異常處理
異常處理:java變成語言使用異常處理機制爲程序提供了錯誤處理的能力
異常處理的關鍵字:
異常實例1:除數與被除數(除數不能爲0)
異常處理
public static void main(String[] args) {
//包裹可能會出現的bug(異常)的代碼
try{
Scanner sc = new Scanner(System.in);
System.out.print("請輸入被除數:");
int a = sc.nextInt();
System.out.print("請輸入除數:");
int b = sc.nextInt();
System.out.println("運算結果:"+a/b);
}catch(InputMismatchException ie){
//輸入內容不匹配的情況,就會進入這個異常處理
System.out.println("輸入的是非整數");
}catch(ArithmeticException ae){
//除數是零的時候,算數出現錯誤異常時,就會進入這個異常處理
System.out.println("除數不能爲0");
}catch(Exception e){
//除了上邊出現的錯誤以外,其他任何異常發生,就會進入到這個異常處理
System.out.println("程序出現未知錯誤");
}finally{
//b不管程序有沒有異常,最終都要執行finally代碼
//finally是最終執行的代碼塊
System.out.println("謝謝使用!");
}
}
異常處理實例2:
public class Ch02 {
public static int test(){
int a;
try{
a = 10;
return a;
}finally{
System.out.println("aaaaaaaaaaa");
a = 20;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(test());
}
}
運行結果:從運行結果可以看出:
try{}裏有一個return語句,
那麼緊跟在這個try後有finally{},
會執行finally代碼,
但會在return之後
在異常處理的關鍵詞中有throw和throws兩個自定義異常
通過兩個實例來解釋兩個關鍵字
1.關鍵字throws
public class Ch03 {
/**
* throws
* 明顯提示異常信息
* 聲明方法可能會出現的異常
* 調用者必須要解決這些異常
* @throws ArithmeticException
* @throws InputMismatchException
* @throws Exception
*/
public static void test() throws ArithmeticException,InputMismatchException,Exception{
Scanner sc = new Scanner(System.in);
System.out.print("請輸入被除數:");
int a = sc.nextInt();
System.out.print("請輸入除數:");
int b = sc.nextInt();
System.out.println("運算結果:"+a/b);
}
public static void main(String[] args) {
try {
test();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
//打印錯誤提示信息
e.printStackTrace();
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.關鍵字throw
public class Ch04 {
/**
* throw
* 不明顯提示異常信息
* 作用:手動拋出異常
* 程序本身可能沒有異常,但是不符合業務邏輯時
* 可以手動拋出異常
* @throws ArithmeticException
* @throws InputMismatchException
* @throws Exception
*/
public static void test(){
Scanner sc = new Scanner(System.in);
System.out.print("請輸入被除數:");
int a = sc.nextInt();
System.out.print("請輸入除數:");
int b = sc.nextInt();
if(b == 1){
throw new CanNotBeOneException();
}
if(b == 0){
throw new ArithmeticException("除數不能爲0");
}
System.out.println("運算結果:"+a/b);
}
public static void main(String[] args) {
test();
}
}
/**
* 自定義異常
* @author Administrator
*
*/
class CanNotBeOneException extends RuntimeException{
public CanNotBeOneException() {
super("除數不能爲1");
// TODO Auto-generated constructor stub
}
}
java中異常的分類:
分爲check異常(必須解決)和運行異常(可以不解決)
下面寫一個實例解釋一下
public class Ch05 {
public void test(int a,int b) throws AException{
//計算兩數之商
if(a == 0){
//runtime異常可以不解決
throw new BException();
}
if(b == 0){
//check異常,必須解決
throw new AException();
}
}
}
/**
* checked異常,必須要解決
* @author Administrator
*
*/
class AException extends Exception{
public AException() {
super();
// TODO Auto-generated constructor stub
}
public AException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
/**
* 運行時異常,可以不解決
* @author Administrator
*
*/
class BException extends RuntimeException{
public BException() {
super();
// TODO Auto-generated constructor stub
}
public BException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
最後說一下異常處理的注意事項: