首页 > 其他分享 >异常处理

异常处理

时间:2022-10-20 20:22:51浏览次数:47  
标签:处理 System try println finally catch 异常 out

1.使用java异常处理机制

把可能会发生错误的代码放进try语句块中。 当程序检测到出现了一个错误时会抛出一个异常对象。

异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。

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

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

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

 

java中的异常分类

Throwable类有两个直接子类: Exception:出现的问题是可以被捕获的; Error:系统错误,通常由JVM处理。

可捕获的异常又可以分为两类:

(1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出

(2)Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象: throw new ArithmeticException(…);

动手动脑:

1.

 AboutException.java

import javax.swing.*;

class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
k=i/j;

try

{
k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}

catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}

catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());
}
}

finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}

 

解析:程序运行到第9行时抛出 java.lang.ArithmeticException: 该异常被catch语句捕获,

 2.多层的异常捕获-1

package 动手动脑;

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

 

 

 3.多层的异常处理-2

package 动手动脑;

public class CatchWho {
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");
}
}
}

 

当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。


public class EmbededFinally {


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");

}

}

}

 

 

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

标签:处理,System,try,println,finally,catch,异常,out
From: https://www.cnblogs.com/qq2143187807/p/16811143.html

相关文章

  • 在Java中需要特殊处理时间的方法
    在开发中会遇到时间的特殊处理例如:时间转换,未来时间(当前时间加上分时天),时间相差1.时间转换有时候时间格式不对,不是我们需要的,那就需要转换,转换成这样的2022-1......
  • 日志数据分析和数据的可视化处理
    数据分析:在HIVE统计下列数据。(1)统计最受欢迎的视频/文章的Top10访问次数(video/article)(2)按照地市统计最受欢迎的Top10课程(ip)(3)按照流量统计最受欢迎的Top10课程(traffic......
  • 自然语言处理学习笔记-lecture5-语言模型01
    基本概念大规模语料库的出现为自然语言统计处理方法的实现提供了可能,一个句子\(s=w_1w_2\cdotsw_m\)的先验概率如下:\[\begin{aligned}p(s)&=p(w_1)\timesp(w_2|w_......
  • 超标量处理器设计 姚永斌 pdf
    清华大学出版社出版,姚永斌著本书讲述超标量(SuperScalar)处理器的设计,现代的高性能处理器都采用了超标量结构,大至服务器和高性能PC的处理器,小至平板电脑和智能手机的处理器,......
  • Redisson 大bug处理: Command xxx succesfully sent, but channel xxx has been close
    棘手的bug,无从下手使用redisson的过程中, 我们的应用程序突然报错: org.redisson.client.RedisConnectionClosedException:Command(EXISTS),params:[paypal_fee_i......
  • [报错] Modelsim远程无法打开异常处理
    在远程桌面打开Modelsim时,我们无法打开,会弹出:FatalLicenseError错误,其原因为:在License文件中,没有允许远程访问;所以我们只需要在Modelsim的license文件中增加允许远程打开......
  • Mysql 系列 | 性能优化 - 紧急临时处理
    在实际开发过程中,业务高峰期常遇到Mysql响应变慢。为了不影响业务,要在短时间内临时提升性能。短链接原因分析短链接是连接数据库后,执行很少的SQL后就断开,下次需......
  • KingbaseES V8R3集群运维案例之---failover故障处理
    案例说明:此案例,为KingbaseESV8R3集群failover切换时,通用的故障处理方式。通过对failover.log和recovery.log日志的解读,让大家了解KingbaseESV8R3集群failover的恢复过程......
  • Mybatis 插入时设置参数异常: Invalid argument value: java.io.NotSerializableExcept
    记录一个因为同事代码生成造成的问题因为代码中大量的自动生成代码,所以看到LongVARCHAR我也没有过多怀疑  最后定位发现还是自动生成的问题,只是原有的生成代码未使......
  • 异常
    异常:异常的处理机制就是把发生错误的时候,把错误信息记录下来,记录的更清楚一些1.异常的用法try{有可能出现异常的语句;}[catch(异常类型异常对象){}...][fi......