Two Types of Error in JAVA

在 java 中有兩種不同的 error, Compile-time Error 和 Runtime Error.

 

Compile-time Error:  發生在程序compile的時候, compile-time error 不能被catch,只是error,不是exception;程序並沒有運行。

造成的原因:

  1. 語法錯誤 

          1.1  ...expected.  括號 ( parentheses, square brackets, curly braces)等沒有匹配或者缺少分號( semicolon)  

          1.2unclosed string literal String 的冒號不匹配 。 

          1.3... 這裏都是簡單的語法上錯誤。 

  2. Identifiers 

          2.1  比如缺少對某個變量的定義  

          2.2  重複對某個變量定義  

          2.3 不能訪問 ...has private access in ..

  3. Computation 

          3.1 使用沒有被賦值的變量對其他變量賦值。 variable ... might not have been initialized. etc.  

          3.2 傳入參數類型不匹配

          3.3 操作符不能進行計算 int a; boolean b; a + b 錯誤。  ..

  4. 返回值 

          4.1 缺少返回值/返回語句  

          4.2 沒有返回類型 

          4.3 不能到達的語句 

  5. 訪問靜態entity

          訪問對象中的方法, 沒有實例化對象,也沒有講方法使用static進行標註。


Runtime Error: 發生在程序運行的時候. 

       Runtime Error 大體上可分爲兩類 - 1. Errors 2. Exceptions.


         它們的結構如下: (紅色塊 是checked exception 藍色塊的是 unckecked exception)


                Unchecked Exception: 就是不需要強制使用 try-catch throw的exception ( RunTime Exception 的所有子類)

                Checked Exception: Exception 的所有除了Runtime Exception 的子類,如果我們不對checked exception處理的時候,compile 根本過不了( 在eclipse上會顯示exception,                       並提示你增加try-catch)。

所以可以根據在編譯的時候編譯器是否會檢查,檢查的話就是checked exception, 反之就是 unchecked.

                                               

從上圖可以看出來,Error 和 Exception 都是繼承 Throwable 類, 那麼說明他們都可以拋出異常。 但是值得注意的是Error不能被try - catch 到, 但是 Exception 能被try - catch 到。 



用戶定義 Exception

以下是一個用戶定義 exception 的例子。 


因爲我們繼承的是Exception, 所以我們定義的這個 exception 是 checked exception

package Exception_Example;

/**
 * Define Exception Class
 * 
 * */

public class InsufficientFundsException extends Exception{
	private double amount;
	
	public InsufficientFundsException (double amount) {
		this.amount = amount;
	}
	
	public double getAmount () {
		return amount;
	}

}



package Exception_Example;

public class CheckingAccount {
	private double balance;
	private int number;
	
	public CheckingAccount (int number) {
		this.number = number;
	}
	
	public void deposit (double money) {
		this.balance += money;
	}
	
	public void withdraw (double amount) throws InsufficientFundsException {
		if (amount <= balance) {
			balance -= amount;
		} else {
			double needs = amount - balance;
			throw new InsufficientFundsException(needs);
		}
	}
	
	public double getBalance () {
		return this.balance;
	}
	
	public int getNumber () {
		return this.number;
	}
}

Main 函數


package Exception_Example;

public class BankDemo {
	public static void main (String[] args) {
		CheckingAccount account = new CheckingAccount(101);
		account.deposit(500);
		//Since we extends from Exception which is checked exception
		//we must handle it before we can compile.
		try{
			account.withdraw(600);
		} catch (InsufficientFundsException e) {
			System.out.println(e + " Less money: " + e.getAmount());
			
		} finally {
			System.out.println("Successfully cache!");
		}
	}

}

因爲這是一個 checked exception, 所以在main函數中我們必須加上try - catch. 

但是如果我們要是想定義一個unchecked 的 exception呢? 

其實就是簡單的講 InsufficientFundsException 的父類改成 RuntimeException, 在這時 main函數可以變成。並且不會報錯( 運行時拋出異常)

<span style="font-size:14px;">package Exception_Example;

public class BankDemo {
	public static void main (String[] args) {
		CheckingAccount account = new CheckingAccount(101);
		account.deposit(500);
		account.withdraw(600);
	}

}</span><span style="font-size:18px;">
</span>



有問題歡迎指出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章