#在千锋“逆战”学习第26天#异常、Question11

总结
概念:程序在运行过程中出现的特殊情况
异常处理的必要性:任何程序都可能存在大量的未知结果、错误;如果不对这些问题进行正确处理,则可能导致程序的中断,造成不必要的损失。

异常的分类
Throwable:可抛出的,一切错误或异常的父类,位于java.lang包中
Error:JVM、硬件、执行逻辑错误,不能手动处理。
Exception:程序在运行和配置中产生的问题,可处理。
RuntimeException:运行时异常,可处理,可不处理。
CheckedException:受查异常,必须处理。

异常的产生
自动抛出异常:当程序运行时,遇到不符合规范的代码或结果时,会产生异常。
手动抛出异常:语法:throw new 异常类型(“实际参数”);
产生异常结果:相当于遇到return语句,导致程序因异常而终止

异常的传递
异常的传递:按照方法的调用链反向传递,如始终没有处理异常,最终会由JVM进行默认异常处理(打印堆栈跟踪信息)

受查异常:throws 声明异常,修饰在方法参数列表后端。

运行时异常:因可处理可不处理,无需声明异常

异常的处理
在这里插入图片描述
常见的异常处理结构
在这里插入图片描述

---------------------------------------------------------------------------------作业分割线

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

  2. 查询 API,填空
    I. 异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于___已检查___(已检查|运行时)异常,从处理方式上说,对这种异常___必须___处理。
    II. 异常类 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. (try-catch-finally)有如下代码:

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

问:当读入的 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

输入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

  1. (try-catch)代码改错。
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();
}
}

在这里插入图片描述
多重catch需要遵循从小到大的原则,所以将两个catch位置互换
m方法中抛出的异常必须是Exception的子类
方法ma必须有返回值,所以把return 100放到finally里面。

  1. (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;
}
}

选择正确答案:
A. 编译不通过
B. 编译通过,输出-1
C. 编译通过,输出 0

答案:A
因为n=10/0赋值异常,捕获异常后并没有对n赋值,n相当于没有被初始化赋值,没法return。

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

在 ma 中,读入整数 b,如果读入的值为 10,则输出: ma1,ma2 1,In Finally 。如果读入的值为 0,则输出: ma1 ,In Finally

  1. (异常的捕获和抛出)有以下代码
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. 编译正常,运行时出错

答案:C
抛出空指针异常下面那行System.out.println(“ma2”);是执行不到的代码,编译会报错。

  1. (异常的捕获和抛出)有如下代码
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){}

答案:A B
在这里插入图片描述
C不能通过是因为SQLException是数据库访问错误或其他提供信息的异常
通常是因为:
1、应用程序无法连接数据库
2、要执行的查询存在语法错误
3、查询中所使用的表/列不存在
4、试图插入或更新的数据违反了数据库的约束

可见try块里的代码是不会发生这些异常的,所以不会抛出SQLException异常,编译不通过。

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