1.填空
Java中所有的错误都继承自(Throwable)类;在该类的子类中,(Error)类表示严重的底层错误,对于这类错误一般处理的方式是(不可处理,不是代码的问题,代码也处理不了);(Exception)类表示例外,异常
2.查询API,填空
Ⅰ,异常类java.rmi.AlreadyBoundException,从分类上说,该类属于(已检查)(已检查|运行时)异常,从处理方式上说,对这种异常(必须)处理。
Ⅱ. 异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于(运行时)(已检查|运行时)异常,从处理方式上说,对这种异常(可处理,可不)处理。
3.(异常的产生)把下面代码补充完整
public class TestThrow {
public static void main(String[] args) throws Exception {
throwException(10);
}
public static void throwException(int n) throws Exception {
if (n == 0) {
Exception e = new NullPointerException();
throw e;
} else {
Exception e = new ClassCastException();
System.out.println("类型转换出错");
throw e;
}
}
}
4.(try-catch-finally)有如下代码:
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
public class TesException {
public static void main(String[] args) {
System.out.println("main 1");
int n;
n=1;//n分别为1,2,3,4,5
ma(n);
System.out.println("main 2");
}
public static void ma(int n) {
try {
System.out.println("ma1");
mb(n);
System.out.println("m2");
}catch(EOFException e) {
System.out.println("Cathc 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
n=2
n=3
n=4
n=5
7.(try-catch)代码改错
public class TestException {
public static void main(String[] args) {
ma();
}
public static int ma() {
try {
m();
return 100;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException");
} catch (Exception e) {
System.out.println("Exception");
}
return 0;
}
public static void m() throws Exception {
throw new MyException();
}
}
class MyException extends Exception {
}
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;//编译不通过,n还没被初始化,无法返回
}
}
编译不通过
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The local variable n may not have been initialized
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 = 10;//
try {
System.out.println("ma1");
int result = n / b;
System.out.println("ma2" + result);
} finally {
System.out.println("In Finally");
}
}
}
在ma中,当读入的b为100时,输出结果为:
ma1
ma21
In Finally
当读入的b为0时,输出结果为:
ma1
In Finally
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.编译出错
14.(异常的捕获和抛出)有如下代码
import java.io.*;
import java.sql.*;
public class TestException {
public static void main(String[] args) {
try {
ma();
}
catch(NullPointerException npe) {}
catch(IOException ioe) {}
/**
* Unreachable catch block for SQLException.
* This exception is never thrown from the try statement body
* SQLException不可达的捕获块。对于SQLException,
* 不会从try语句不可达的catch块抛出此异常。这个异常永远不会从try语句主体中抛出
*/
//catch(SQLException sqle) {}//编译报错
catch(Exception e) {
}
}
public static void ma()throws IOException{}
}
AB可编译通过