Key Point:
- 異常的概念和分類
- 異常的產生和傳遞
- 異常的處理
- 自定義異常
問題:
1.填空
Java中所有的錯誤都繼承自____類;
在該類的子類中,____類表示嚴重的底層錯誤,對於這類錯誤一般的處理方式是____;
____類表示例外、異常。
//Throwable
//Error 不可處理
//Exception
2.查詢API,填空
I.異常類java.rmi.AlreadyBoundException,從分類上說,該類屬於____(已檢查|運行時)異常,從處理方式上說,對這種異常____處理
II.異常類java.util.regex.PatternSyntaxException,從分類上說,該類屬於____已檢查|運行時)異常,從處理方式上說,對這種異常____處理
//已檢查 必須 (AlreadyBoundException extends java.lang.Exception)
//運行時 可處理可不 (extends IllegalArgumentException,IllegalArgumentException extends RuntimeException)
3.(異常的產生)把下面代碼補充完整
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n) {
if(n == 0) {
//拋出一個NullPointerException
throw new NullPointerException();
}else {
//拋出一個ClassCastException
//並設定詳細信息爲"類型轉換出錯"
throw new ClassCastException("類型轉換出錯");
}
}
}
4.(try-catch-finally)有如下代碼:
import java.io.*;
import java.sql.*;
public class TestException {
public static void main(String[] args) {
System.out.println("main 1");
int n=4;
//讀入n
ma(n);
System.out.println("main2");
}
public static void ma(int n) {
try {
System.out.println("ma1");
mb(n);
System.out.println("ma2");
}catch(EOFException e) {
System.out.println("Catch EOFException");
}catch(IOException e) {
System.out.println("Catch IOException");
}catch(SQLException e) {
System.out.println("Catch SQLException");
}catch(Exception e) {
System.out.println("Catch Exception");
}finally {
System.out.println("In finally");
}
}
public static void mb(int n)throws Exception{
System.out.println("mb1");
if(n==1) throw new EOFException();
if(n==2) throw new FileNotFoundException();
if(n==3) throw new SQLException();
if(n==4) throw new NullPointerException();
System.out.println("mb2");
}
}
問:當讀入的n分別爲1,2,3,4,5時,輸出的結果分別是什麼?
//1
main 1
ma1
mb1
Catch EOFException
In finally
main2
//2
main 1
ma1
mb1
Catch IOException
In finally
main2
FileNotFoundException()是Catch IOException的子類
//3
main 1
ma1
mb1
Catch SQLException
In finally
main2
//4
main 1
ma1
mb1
Catch Exception
In finally
main2
//5
main 1
ma1
mb1
mb2
ma2
In finally
main2
5.(自定義異常)創建兩個自定義異常類MyException1和MyException2.
要求:
I.MyException1爲已檢查異常,MyException2爲運行時異常。
II.這兩個異常均具有兩個構造函數,一個無參,另一個帶字符串參數,參數表示產生異常的信息。
public class TestDefinedException {
public static void main(String[] args) {
}
}
//受查異常
class MyException1 extends Exception {
public MyException1() {}
public MyException1(String message){
super(message);
}
}
//運行時異常
class MyException2 extends RuntimeException{
public MyException2() {}
public MyException2(String message) {
super(message);
}
}
6.(自定義異常)在上一題的基礎上,把下面代碼補充完整。
import java.util.Scanner;
public class TestDefinedException {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
//讀入n
try {
m(n);
}catch(MyException1 ex1) {
//輸出ex1詳細的方法調用棧信息
ex1.printStackTrace();
}catch(MyException2 ex2) {
//輸出ex2的詳細信息
System.err.println(ex2.getMessage());
//並把ex2重新拋出
throw ex2;
}
}
public static void m(int n) throws MyException1{//聲明拋出MyException1
if(n == 1) {
//拋出MyException1
//並設定其詳細信息爲“n==1”
throw new MyException1("n==1");
}else {
//拋出MyException2
//並設定其詳細信息爲“n==2”
throw new MyException2("n==2");
}
}
}
//受查異常
class MyException1 extends Exception {
public MyException1() {}
public MyException1(String message){
super(message);
}
}
//運行時異常
class MyException2 extends RuntimeException{
public MyException2() {}
public MyException2(String message) {
super(message);
}
}
7.(try-catch)代碼改錯
class MyException{}
class TestException {
public static void main(String[] args) {
ma(2);
}
public static int ma() {
try {
m();
return 100;
}
catch(ArithmeticException e) {
System.out.println("ArithmeticException");
return 0 ;
}catch(Exception e) {//順序
System.out.println("Exception");
return 0 ;
}
}
public static void m(){
/*throw*/ new MyException();
}
}
8.(方法覆蓋)有如下代碼
import java.io.IOException;
class Super{
public void ma() throws IOException{}
}
interface IA{
void mb();
}
public class MySub extends Super implements IA{
public void ma() {}//1
public void mb() {}//2
}
問:在//1處,填入以下____代碼可以編譯通過,在//2處,填入______代碼可以編譯通過。
A.throws java.io.IOException
B.throws java.io.FileNotFoundException,java.io.EOFException
C.throws java.sql.SQLException
D.不能拋出任何異常
//AB
//D
9.(try-catch,局部變量)有如下代碼
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma() {
int n;
try {
n=10/0;
}catch(Exception e) {
}
return n ;
}
}
編譯不通過
10.(try-catch-finally)有如下代碼
public class TestFinally {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma() {
int b;
//讀入b
try {
int n = 100;
return n/b;
}catch(Exception e) {
return 10;
}finally {
return 100;
}
}
}
在ma中,當讀入的b爲100時,輸出的結果爲_____,當讀入的b爲0時,輸出的結果爲_____。
//100
//100
11.(try-finally)寫出下面代碼運行的結果
public class TestTryFinally {
public static void main(String[] args) {
try {
ma();
}catch(Exception ex1) {
}
}
public static void ma() throws Exception{
int n = 10;
int b;
//讀入一個整數b
try {
System.out.println("ma");
int result = n/b;
System.out.println("ma2"+result);
}finally {
System.out.println("In Finally");
}
}
}
在ma中,讀入整數b,如果讀入的值爲10,則輸出:_____。
如果讀入的值爲0.則輸出:_____。
//ma
ma21
In Finally
// ma
In Finally
12.(方法覆蓋)
import java.io.*;
class MySuper {
public void m() throws IOException{}
}
class MySub extends MySuper{
public void m() throws EOFException{}
}
class MySub2 extends MySub{
public void m() throws FileNotFoundException{}
}
以上代碼是否能編譯通過?如果不能,應該如何修改?
編譯不能通過 ,如以下使MySub2的父類擴大範圍則可
import java.io.*;
class MySuper {
public void m() throws IOException{}
}
class MySub extends MySuper{
public void m() throws EOFException,FileNotFoundException{}
}
class MySub2 extends MySub{
public void m() throws FileNotFoundException{}
}
13.(異常的捕獲和拋出)有如下代碼
public class TestException {
public static void main(String[] args) {
try {
System.out.println("main1");
ma();
System.out.println("main2");
}catch(Exception e) {
System.out.println("In Catch");
}
}
public static void ma() {
System.out.println("ma1");
throw new NullPointerException();
System.out.println("ma2");
}
}
編譯出錯
14(異常的捕獲和拋出)有如下代碼
import java.io.*;
import java.sql.*;
class TestException {
public static void main(String[] args) {
try {
ma();
}
/*1*/
catch(Exception e) {
}
}
public static void ma() throws IOException{}
}
下面那些代碼放在/*1*/處可以編譯通過?
A.catch(NullPointerException npe){}
B.catch(IOException ioe){}
C.catch(SQLException sqle){}
AB可以 C爲受查異常