C/C++教程

pratice13

本文主要是介绍pratice13,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Question13_异常

1、Java 中所有的错误都继承自______类;在该类的子类中,类表示严重的底层错误,对于这类错误一般处理的方式是;______类表示例外、异常。

2、查询API,填空

2.1 异常类java.rmi.AlreadyBoundException,从分类上说,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
2.2 异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。

3、把下面代码补充完整

public class TestThrow {
	public static void main(String args[]) {
		throwException(10);
	}

	public static void throwException(int n) {
		if (n == 0) {
			//抛出一个NullPointerException
		} else {
			//抛出一个ClassCastException
			//并设定详细信息为“类型转换出错”
		}
	}
}

4、有如下代码,问:当读入的n分别为1,2,3,4,5 时,输出的结果分别是什么?

import java.io.*;
import java.sql.*;

class TestException {
	public static void main(String args[]){
		System.out.println("main 1");
		int n;
		//读⼊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");
	}
}

5、创建两个自定义异常类MyException1和MyException2

5.1 MyException1为已检查异常,MyException2为运行时异常。
5.2 这两个异常均具有两个构造函数,一个无参,另一个带字符串参数,参数表示产生异常的信息。

6、在上一题的基础上,把下面代码补充完整

public class TestMyException {
	public static void main(String args[]){
		int n;
		//读⼊n
		try{
			m(n);
		}catch(MyException1 ex1){
			//输出ex1 详细的⽅法调⽤栈信息
		}catch(MyException2 ex2){
			//输出ex2 的详细信息
			//并把ex2 重新抛出
		}
	}

	public static void m(int n)_________ { //声明抛出MyException1
		if (n == 1) {
			//抛出MyException1
			//并设定其详细信息为“n == 1”
		}else {
			//抛出MyException2
			//并设定其详细信息为“n == 2”
		}
	}
}

7、代码改错

class MyException {
}

class TestException {
	public static void main(String args[]) {
		ma();
	}

	public static int ma(){
		try{
			m();
			return 100;
		}catch(Exception e){
			System.out.println("Exception");
		}catch(ArithmeticException e){
			System.out.println("ArithmeticException");
		}
	}

	public static void m() {
		throw new MyException();
	}
}

8、有如下代码,在//1 处,填入以下______代码可以编译通过,在//2 处,填入__代码可以编译通过。

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______{}
}

A. throws java.io.IOException
B. throws java.io.FileNotFoundException, java.io.EOFException
C. throws java.sql.SQLException
D. 不能抛出任何异常。

9、有如下代码,选择正确答案:

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;
	}
}

A. 编译不通过

B. 编译通过,输出-1

C. 编译通过,输出0

10、有如下代码,在ma中,当读入的b为100时,输出结果为_____,当读入的b为0时,输出结果为______。

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;
		}
	}
}

11、写出下面代码运行的结果,在ma中,读入整数b,如果读入的值为10,则输出: __ 。如果读入的值为0,则输出: __ 。

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("ma1");
			int result = n / b;
			System.out.println("ma2" + result);
		}finally{
			System.out.println("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 {
	}
}

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");
	}
}

A. 编译出错
B. 编译正常,输出main1 ma1 In Catch
C. 编译正常,运行时出错

14、有如下代码,下面哪些代码放在/1 /处可以编译通过?

A. catch(NullPointerException npe){}
B. catch(IOException ioe){}
C. catch(SQLException sqle){}

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 {
	}
}
这篇关于pratice13的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!