java異常基礎知識點
阿新 • • 發佈:2019-02-13
一、異常的產生和捕獲:
package Exception;
/**
* 演示了java中異常的產生和捕獲
* @firstmiki
*/
import java.util.Scanner;
public class Exception1 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
try{
String str = "123a";
int output = Integer.parseInt(str); //這一句就產生了異常
System.out.println(output);
}
catch(NumberFormatException e){
System.out.println(e.getMessage());
/*System.out.println(e.getClass());
System.out.println(e.getStackTrace());*/
e.printStackTrace();
}
}
}
執行結果:
For input string: "123a"
java.lang.NumberFormatException: For input string: "123a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Exception.Exception1.main(Exception1.java:15)
二、異常語句:try...catch...finally...
注意下面程式碼中的16--19行:
package Exception;
/*
* finally語句 (重要)
*/
public class TestFinally {
public static void testfinally(){
String str = "123a";
try{
int a = Integer.parseInt(str);
System.out.println(a);
}catch(Exception e){
e.printStackTrace();
System.out.println("exception");
return; //返回,不再執行下面語句;
}finally{
//儘管加了return,奇怪的是,這裡的finally語句被執行了;
System.out.println("the end in finally");
}
System.out.println("end");
}
public static void main(String[] args) {
testfinally();
}
}
執行結果:
java.lang.NumberFormatException: For input string: "123a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Exception.TestFinally.testfinally(TestFinally.java:10)
at Exception.TestFinally.main(TestFinally.java:25)
exception
the end in finally
三、throws關鍵字:
注意看下面的註釋說明:
package Exception;
/*throws是方法可能丟擲異常的宣告。(用在宣告方法時,表示該方法可能要丟擲異常)
當某個方法可能會丟擲某種異常時用於throws 宣告可能丟擲的異常,然後交給上層呼叫它的方法程式處理。*/
public class Testthrows {
//throws:把異常向外面拋
public static void testthrows() throws NumberFormatException{
String str = "123a";
int a = Integer.parseInt(str);
System.out.println(a);
}
public static void main(String[] args) {
try{
testthrows();
}catch(Exception e){
System.out.println("我們在這裡處理了一個異常");
e.printStackTrace();
}
System.out.println("這裡還是被執行了");
}
}
執行結果:我們在這裡處理了一個異常
java.lang.NumberFormatException: For input string: "123a"這裡還是被執行了
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Exception.Testthrows.testthrows(Testthrows.java:8)
at Exception.Testthrows.main(Testthrows.java:14)
四、throw關鍵字:
package Exception;
/*throw是語句丟擲一個異常。語法:throw (異常物件); 如: throw e;
一般會用於程式出現某種邏輯時程式設計師主動丟擲某種特定型別的異常。*/
public class Testthrow {
public static void testthrow(int a) throws Exception{
if(a==1){
//直接丟擲一個異常
throw new Exception("這裡a有異常");
}
System.out.println("這裡a沒有異常,a="+a);
}
public static void main(String[] args) {
try {
testthrow(1);
} catch (Exception e) {
e.printStackTrace();
}
try {
testthrow(0);
}catch (Exception e) {
e.printStackTrace();
}
}
}
執行結果:
java.lang.Exception: 這裡a有異常
at Exception.Testthrow.testthrow(Testthrow.java:8)
at Exception.Testthrow.main(Testthrow.java:14)
這裡a沒有異常,a=0