# 大數據學習筆記第9天 - 異常、IDE #
## 01 回顧 作業
...
## 02 異常分類 異常捕獲
異常體系
常見異常
異常的處理
自定義異常類及使用
常見的異常:被0除,空指針,索引越界等;
異常的體系:
Throwable
Error
通常出現重大問題,如虛擬機崩潰或者內存溢出等;
這些異常不用處理,也處理不了
Exception
在運行時出現的異常情況,可以通過try catch finally處理
Exception和Error的子類名都是以父類名作為后綴
圖exception01.jpg
圖exception02.jpg
圖exception03.jpg
圖exception04.jpg
圖exception05.jpg
圖exception06.jpg
圖exception07.jpg
圖exception08.jpg
演示實例:當除數為0時,java.lang.ArithmeticException: / by zero
*E:\01\01 Java\day09\code\ExceptionDemo01.java*
[code]
public class ExceptionDemo01{
public static void main(String[] args){
int i = 2 / 0; //編譯時不報錯,運行時異常
}
}
[/code]
圖2018-08-28_224801.png
演示實例:捕獲異常
圖2018-08-28_230630.png
*E:\01\01 Java\day09\code\TryDemo.java*
[code]
/*
try...catch捕獲異常
*/
public class TryDemo{
public static void main(String[] args){
try{
int num = 1/0;
}catch(ArithmeticException e){
System.out.println("發生了算術異常");
}catch(NullPointerException e){
System.out.println("發生了空指針異常");
}finally{
System.out.println("一定會被執行后的代碼");
}
System.out.println("異常處理之后的代碼");
}
}
[/code]
如果程序沒有捕獲到異常,將不會往下執行,如下所示:
[code]
/*
try...catch捕獲異常
*/
public class TryDemo{
public static void main(String[] args){
try{
//超出索引范圍的異常
int[] arr={1,2,3};
int i=arr[3];
}catch(ArithmeticException e){
System.out.println("發生了算術異常");
}catch(NullPointerException e){
System.out.println("發生了空指針異常");
}
//java.lang.ArrayIndexOutOfBoundsException
/*
catch(Exception e){
System.out.println("發生了未知異常");
}
*/
finally{
System.out.println("一定會被執行后的代碼");
}
System.out.println("異常處理之后的代碼");
}
}
[/code]
圖2018-08-28_231342.png
## 03 finally執行流程面試題
catch塊中常見的處理方法:
- 由于捕獲的異常都是Throwable類的子類對象,所以完全可以調用Throwable中定義的方法。
- getMessage() 獲取異常信息,返回字符串。
- toString() 獲取異常類名和異常信息,返回字符串。
- printStackTrace() 獲取異常類名和異常信息,以及異常出現在程序中的位置,返回值void。
- printStackTrace(PrintStream s) 通常用該方法將異常內容保存在日志文件中。
圖2018-08-28_235800.png
實例:
*E:\01\01 Java\day09\code\TryDemo2.java*
[code]
public class TryDemo2{
public static void main(String[] args){
try{
int[] arr={1,2,3};
int i=arr[3];
}catch(Exception e){
String message = e.getMessage();
System.out.println(message);
System.out.println();
e.printStackTrace();
System.out.println();
String message2 = e.toString();
//java.lang.ArrayIndexOutOfBoundsException: 3
System.out.println(message2);
}finally{
System.out.println("一定會被執行后的代碼");
}
System.out.println("異常處理之后的代碼");
}
}
[/code]
圖2018-08-29_002005.png
面試題:finally的執行流程
*E:\01\01 Java\day09\code\FinallyTest.java*
[code]
public class FinallyTest{
public static void main(String[] args){
int num = test();
System.out.println("test()方法的返回值是:" + num);
}
public static int test(){
try{
System.out.println("進入了test()方法");
int i=1/0;
return 1;
}catch(Exception e){
System.out.println("該程序發生了異常");
return 2; //暫時掛起,等執行完finally再執行
}finally{
System.out.println("finally里的語句");
return 3; //一般不會再finally中定義return,提前終止程序
}
}
}
[/code]
圖2018-08-29_001407.png
## 04 throws throw關鍵字區別
## 05 自定義異常類及使用
## 06 自定義異常類練習
## 07 方法重寫時異常處理的細節
## 08 包簡介 定義格式
## 09 訪問權限演示
## 10 修飾符總結
## 11 jar包的制作和使用
## 12 eclipse基本操作
## 13 子類實現抽象類 接口的快速方法
## 14 使用第三方jar包
## 15 項目的導入