首页 > 编程语言 >Java语言程序设计第五讲,异常处理

Java语言程序设计第五讲,异常处理

时间:2022-10-22 01:33:08浏览次数:41  
标签:ArrayIndexOutOfBoundsException Java System try println 第五 catch 程序设计 out

学习Java变成过程中我们遇到过许多程序报错现象

之后我们会查看报错原因,对代码进行有针对性的修改从而使其恢复正常

 

这就是异常处理的目的和用途:计算机提供准确的错徐信息,操作者则根据实际情况提供错误的应对策略与手段

 

在具体学习异常之前先来了解一下Java的异常处理机制:

1,操作者把可能会发生错误的代码放进try语句块中

2,当程序检测到出现了一个错徐时会抛出一个异常对象,异常处理代码会捕获并处理这个错误(catch语句块中的代码用于处理错误)

3,当异常发生时,程序控制流程有try语句跳转到catch语句块

4,不管是否有异常发生,finally语句块中的语句始终保证被执行

5,如果没有提供合适的异常处理代码JVM会结束掉整个应用程序

 

异常大体可以分为Error和Exception

Error一般指系统报错,比如打印时没有连接打印机等,通常由JVM处理

Exception又可以分为两类,Check异常和Runtime异常

Check异常是直接派生自Exception的异常,必须被捕获或再次声明抛出

Runtime异常是派生自RuntimeException的异常,使用throw语句可以随时抛出这种异常对象

 

assert语句允许程序在运行过程中判断某个条件是否满足,不满足时抛出AssertionError

例子: int i = 3;assert i == 10;//抛出异常

OOM Error 指系统内存不足

浮点数在进行除法时允许出现3.7/0,会抛出infinity但是程序继续执行

 

代码:

public class Main {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}

throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

运行结果:

 

 

系统捕获ArrayIndexOutOfBoundsException();异常后跳转到catch执行System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");语句,之后捕获ArithmeticException();异常,跳转到catch执行System.out.println("发生ArithmeticException");语句

代码:

public class Main {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}

运行结果:

 

 

系统捕获ArrayIndexOutOfBoundsException();异常后为找到与之匹配的catch,于是跳转到外层寻找与之匹配的catch,执行System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");语句 

代码:

public class Main {
public static void main(String args[]) {
int result;
try {
System.out.println("in Level 1");
try {
System.out.println("in Level 2");
// result=100/0; //Level 2
try {
System.out.println("in Level 3");
result=100/0; //Level 3
}
catch (Exception e) {
System.out.println("Level 3:" + e.getClass().toString());
}
finally {
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e) {
System.out.println("Level 2:" + e.getClass().toString());
}
finally {
System.out.println("In Level 2 finally");
}
// result = 100 / 0; //level 1
}
catch (Exception e) {
System.out.println("Level 1:" + e.getClass().toString());
}
finally {
System.out.println("In Level 1 finally");
}
}
}

运行截图:

 

 

 在第三层遇到result=100/0;并抛出异常之后调用catch输出System.out.println("Level 3:" + e.getClass().toString())语句,之后catch语句不再执行,执行完所有的finally语句后结束程序运行

特别,当有多层finally语句嵌套时,异常在不同层次抛出,在不同位置抛出,肯导致不同的finally语句执行顺序

例如上一段代码如果异常在level2 抛出,则运行结果为:

 

 代码:

public class Main {
public static void main( String args[] ) {
try {
method1();
}
catch ( Exception e ) {
System.err.println( e.getMessage() + "\n" );
e.printStackTrace();
}
}
public static void method1() throws Exception {
method2();
}
public static void method2() throws Exception {
method3();
}
public static void method3() throws Exception {
throw new Exception( "Exception thrown in method3" );
}
}

运行结果:

 

 说实话看到运行结果的时候有被吓到……

言归正传,method1();抛出异常,之后printStackTrace()按照method1();被调用的吮吸一次抛出异常路径

 

在throws语句中声明的异常成为受控的异常,通常直接派生自Exception类

RuntimeException(其基类为Exception)和Error(其基类为Throwable)称为非受控的异常,这种异常不用在throws语句中声明

一个方法可以抛出多个受控的异常,此时在此方法调用语句处只要catch其中任何一个异常代码就可以顺利编译

子类的throws语句抛出的异常不能是其基类同名方法抛出的异常对象的父类

public class CatchWho {     public static void main(String[] args) {         try {             try {                 throw new ArrayIndexOutOfBoundsException();             }             catch(ArrayIndexOutOfBoundsException e) {                System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");             }             throw new ArithmeticException();         }         catch(ArithmeticException e) {             System.out.println("发生ArithmeticException");         }         catch(ArrayIndexOutOfBoundsException e) {            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");         }     } }

标签:ArrayIndexOutOfBoundsException,Java,System,try,println,第五,catch,程序设计,out
From: https://www.cnblogs.com/yansans/p/16815178.html

相关文章

  • java基础知识
    JAVA基础知识标识符和关键字关键字标识符无论类名、方法名、变量名都是标识符命令规则:所有标识符都应该以大小写字符、美元符号、或下划线开始首字符后可以......
  • Java程序设计语言第四讲,继承与多态
    Java中通过extends实现继承并且,Java中只允许单继承,从而避免里多继承中可能产生的父类属性冲突问题。子类自动拥有父类声明的public和protected的成员。 继承条件下的......
  • 2022-2023-1 20221312 《计算机基础与程序设计》第八周学习总结
    作业信息班级链接:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK08作业目标:面向对象,面向过程,顶点,......
  • 不妨试试更快更小更灵活Java开发框架Solon
    @目录概述定义性能架构实战SolonWeb示例SolonMybatis-Plus示例SolonWebSocket示例SolonRemotingRPC示例SolonCloudNacos示例概述定义Solon官网地址https://so......
  • 2022-2023-1 20221408《计算机基础与程序设计》第八周学习总结
    第八周学习总结作业信息这个作业属于哪个课程:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP这个作业的要求在哪里:https://www.cnblogs.com/rocedu/p/9577842......
  • 手写基于Java RMI的RPC框架
    留给读者其中最大的区别就是ZooKeeper注册中心,注册中心可以有读写监听器,这是一个优势,可以用来实现订阅通知,也能做数据的同步,甚至可以做基于读写分离的RPC框架,而且它是基......
  • JavaScript实现数据结构 -- 集合
    集合集合是一种无序且唯一的数据结构,在ES6中有集合Set。集合的常用操作去重使用Set结合展开运算符实现数组去重。判断元素是非在集合中使用Set的has方法判断元素是......
  • JavaScript实现数据结构 -- 字典
    字典字典与集合类似,也是一种存储唯一值的数据结构,字典以键值对的形式进行存储,在ES6中有字典Map。字典的常用操作增使用set()方法可以向字典中添加新成员,可连续添加。......
  • JAVA基本类型和包装类型
    JAVA基本类型和包装类型前言Java语言中的数据类型分为基本数据类型和引用类型,而我们进行Java开发的时候都听说过基本数据类型和包装类型,今天我们就来详细聊一聊Java中的......
  • Java中Final、 finally 、finalize的区别
    1、final可以修饰类、变量、方法,修饰类表示该类不能被继承、修饰方法表示该方法不能被重写、修饰变量表示该变量是一个常量不能被重新赋值。2、finally一般作用在try-catch......