知识总结
异常
- 概念:程序在运行过程中出现的特殊情况
- 异常处理的必要性:任何程序都可能存在大量未知问题、错误;如果不及时进行处理,则可能导致程序的中断,造成不必要的损失。
- 异常的分类:
(1)|-Throwable:可抛出的,一切错误或异常的父类,位于java.lang包中。
(2)|-Error:JVM、硬件、执行逻辑错误,不能手动处理。
(3)|-Exception:程序在运行和配置过程中产生的问题,可处理
|-RuntimeException:运行时异常,可处理,可不处理
|-CheckedException:受查异常,必须处理 - 异常的产生:
(1)自动抛出异常:当程序在运行时遇到不符合规范的代码或结果时,会产生异常。
(2)手动抛出异常,throw new异常类型(“实际参数”);
(3)一旦产生异常,相当于return语句,导致程序因异常而终止。 - 异常的传递
(1)按照方法的调用链反向传递,如果最终都没有处理异常,最终交由我们的JVM进行默认异常处理(打印堆栈跟踪信息)
(2)受查异常:throws声明异常,声明位置:修饰在方法参数列表的后端
(3)运行时异常:因其可处理,可不处理,无需声明 - try{
可能出现异常的代码
}catch(Exception e){
异常处理的相关代码
}finally{
无论是否出现异常,都执行的代码结构
} - 常用异常处理结构
try{ } catch { }
try{ } catch { } catch { }
try{ } catch { } finally{ }
try{ } catch { } catch { } finally{ }
try{ } finally{ }
注:多重catch,遵循从小到大的顺序,父类异常在最后
每日练习
- 填空
Java中所有的错误都继承自_____类;在该类的子类中,_____ 类表示严重的底层错误,对于这类错误一般处理的方式是______;______类表示例外、异常。
答:Throwable、Error、不处理、Exception - 查询API,填空
(1)异常类java.rmi.AlreadyBoundException,从分类上,该类属于______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
(2)异常类java.util.regex.PatternSyntaxException,从分类上说,该类属于_______(已检查|运行时)异常,从处理方式上说,对这种异常______处理。
答:已检查、必须
运行时、可处理或可不 - 把下面代码补充完整
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n){
if(n == 0){
//抛出一个NullPointException
}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{
try{
throw new ClassCastException();
}catch(ClassCastException e){
System.out.println("类型转换出错");
}
//抛出一个ClassCastException
//并设定详细信息为“类型转换出错”
System.out.println();
}
}
}
- 有如下代码
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("main 2");
}
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时,输出的结果分别是什么?
答:
当n为1时:
main 1
ma1
mb1
Catch EOFException
In finally
main 2
当n为2时:
main 1
ma1
mb1
Catch IOException
In finally
main 2
当n为3时:
main 1
ma1
mb1
Catch SQLException
In finally
main 2
当n为4时:
main 1
ma1
mb1
Catch Exception
In finally
main 2
- 代码改错
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();
}
}
答:
class MyException{}
public class TestException {
public static void main(String[] args) {
ma();
}
public static int ma(){
try{
m();
}catch(ArithmeticException e){
System.out.println("ArithmeticException");
}catch(Exception e){
System.out.println("Exception");
}finally{
return 100;
}
}
public static void m(){
throw new ArithmeticException();
}
}
- 有如下代码
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
- 有如下代码
public class TestTryFinally {
public static void main(String[] args) {
try{
ma();
}catch(Exception ex1){
}
}
public static int 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,则输出:。如果读入的值为0,则输出:_。
答:
ma1
ma21
In Finally
ma1
In Finally
- 有下面代码
public class TestException {
public static void main(String[] args) {
try{
System.out.println("main 1");
ma();
System.out.println("main 2");
}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
- 有如下代码
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 aqle)[}
答:AB