## **Throw**
throw:動詞,真正的拋出一個異常對象
throws:形容詞 ,形容方法的,表示某個方法可能拋出某個異常,要求方法者去調用它
## **不處理,只拋出(可能會拋出異常)**
~~~
//不處理,只拋出(可能會拋出異常)
public class Exception03 {
public static void main(String[] args) throws FileNotFoundException {
FileReader fr = new FileReader("1.txt");
}
}
~~~
### **2.捕獲處理**
~~~
語法格式
try {
//可能出現異常的代碼
} catch (FileNotFoundException e) {
//異常處理
}finaily{
//寫出必須喲啊執行的代碼
//釋放資源
}
public class Exception03 {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("1.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//加上finally
public class Exception03 {
public static void main(String[] args) {
FileReader fr =null;
try {
fr = new FileReader("1.txt");
int ch = fr.read();
} catch (java.lang.Exception ioe) {
System.out.println("文件讀取失敗");
}finally {
try {
fr.close();
} catch (IOException e) {
System.out.println("關閉文件失敗");
}
}
}
}
~~~
3.
* 多個異常,分別處理
* 一次捕獲,多次處理
* 一次捕獲,一次處理(常用)