异常(练习题)

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为受查异常

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