千锋逆战班学习第26天
千锋逆战班学习第26天
努力或许没有收获,但不努力一定没收获,加油。
今天我学了Java课程的异常。
中国加油!!!武汉加油!!!千锋加油!!!我自己加油!!!
总结-异常
一、概念与必要性
概念:程序在运行过程中出现的特殊情况。
异常处理的必要性:任何程序都可能存在大量的未知问题、错误;如果不对这些问题进行正确处理, 则可能导致程序的中断,造成不必要的损失。
二、异常的分类
- Throwable: 可抛出的,一切错误或异常的父类,位于java.lang包中。
- Error:JVM、硬件、执行逻辑错误,不能手动处理。
- Exception:程序在运行和配置中产生的问题,可处理。
- RuntimeException:运行时异常,可处理,可不处理。
- CheckedException:受查异常,必须处理。
三、异常的产生:
- 自动抛出异常:当程序在运行时遇到不符合规范的代码或结果时,会产生异常。
- 手动抛出异常:语法:throw new 异常类型(“实际参数”)。
- 产生异常结果:相当于遇到return语句,导致程序因异常而终止。
四、异常的传递:
- 异常的传递:按照方法调用链反向传递,如始终没有处理异常,最终会由JVM进行默认异常处理(打印堆栈跟踪信息)。
- 受查异常:throws 声明异常,修饰在方法参数列表后端。
- 运行时异常:因可处理可不处理,无需声明异常。
五、异常的处理
try{
可能出现异常的代码
}catch(Exception e){
异常处理的相关代码,如:
}finally{
无论是否出现异常,都需执行的代码结构,常用于释放资源。
}
六、常见异常处理结构
- try{ } catch{ }
- try{ } catch{ } catch{ }
- try{ } catch{ } finally{ }
- try{ } catch{ } catch{ } finally{ }
- try{ } finally{ }
- 注意:多重catch,遵循从子(小)到父(大)的顺序,父类异常在最后。
Question_11_1
Java 中所有的错误都继承自______类;在该类的子类中,类表示严重的底层错误,对于这类错误一般处理的方式是;______类表示例外、异常。
Throwable;Error;不处理;Exception_
Question__11_2
查询 API,填空
I. 异常类 java.rmi.AlreadyBoundException,从分类上说,该类属于 ——(已检查|运行时) 异常,
从处理方式上说,对这种异常______处理。
II.异常类 java.util.regex.PatternSyntaxException,从分类上说,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
- 已检查;必须
- 运行时;可处理可不处理
Question_11_3
代码补充完整:
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n){
if (n == 0){
//抛出一个 NullPointerException;
}else{
//抛出一个 ClassCastException
//并设定详细信息为“类型转换出错”
}
}
}
补充后
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("类型转换出错");
}
}
}
Question_11_4
代码如下
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
public 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)n = 1
main 1
ma1
mb1
Catch EOFException
In finally
main2
(2)n = 2
main 1
ma1
mb1
Catch IOException
In finally
main2
(3)n=3
main 1
ma1
mb1
Catch SQLException
In finally
main2
(4)n = 4
main 1
ma1
mb1
Catch Exception
In finally
main2
(5)n = 5
main 1
ma1
mb1
mb2
ma2
In finally
main2
Question_11_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("ArithmeticException");
}catch(ArithmeticException e){
System.out.println("Exception");
}
}
public static void m(){
//throw new MyException();
}
}
- MyException必须继承Throwable或其子类。
- catch(Exception e)要在catch(ArithmeticException e)后面
- try-catch语句后没有return语句。
Question_11_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
答:A
Question_11_11
写出下面代码运行的结果
public class TestTryFianlly {
public static void main(String args[]){
try{
ma();
}catch(Exception ex1){}
}
public static void ma() throws Exception {
int n = 10;
int b = 0;
//读入一个整数 b
try{
System.out.println("ma1");
int result = n / b;
System.out.println("ma2 " + result);
}finally{
System.out.println("In Finally");
}
}
}
在 ma 中,读入整数 b,如果读入的值为 10,则输出: ______ 。如果读入的值为 0,则输出: ______ 。
(1)
ma1
ma2 1
In Finally
(2)
ma1
In Finally
Question_11_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.编译正常,运行时出错
答:A
Question_11_14
有如下代码
import java.io.IOException;
import java.sql.*;
public 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
如有错误,请多包涵。