java异常处理动手动脑问题

动手动脑01

源程序:

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

程序截图:

 

动手动脑:多层的异常捕获-1

源程序:

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

程序截图:

 

动手动脑:多层的异常捕获-2

源程序:

public class CatchWho2 {
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的执行时机

代码示例:

执行结果:

 

注意:

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

动手动脑--------

辨析:finally语句一定会1执行吗?

示例代码:

执行结果: 

总结:
(1)try语句没有被执行到,如在try语句之前return就返回了,这样finally语句就不会执行。这也说明了finally语句被执行的必要而非充分条件是:相应的try语句一定被执行到。

(2)在try块|catch块中有System.exit(0);这样的语句。System.exit(0)是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。

在try-catch-finally中, 当return遇到finally,return对finally无效,即:

     1.在try catch块里return的时候,finally也会被执行。

     2.finally里的return语句会把try catch块里的return语句效果给覆盖掉。

结论:return语句并不一定都是函数的出口,执行return时,只是把return后面的值复制了一份到返回值变量里去了。(总结部分来源于网络)

 验证:

由图可知不能通过编译

但是修正代码后得以编译

为什么?

throws语句表明某种方法可能出现某种异常,而它自己不能处理,而需要由调用者来处理

 

转载于:https://www.cnblogs.com/lovema1210/p/7846186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用BigDecimal类来进行精确的浮点数运算。BigDecimal类提供了各种方法来执行加法、减法、乘法和除法等运算。它可以避免浮点数运算中的精度丢失问题。 以下是一个使用BigDecimal类进行加法运算的示例代码: import java.math.BigDecimal; public class BigDecimalExample { public static void main(String\[\] args) { BigDecimal num1 = new BigDecimal("0.05"); BigDecimal num2 = new BigDecimal("0.01"); BigDecimal sum = num1.add(num2); System.out.println("0.05 + 0.01 = " + sum); } } 这段代码中,我们创建了两个BigDecimal对象num1和num2,分别表示0.05和0.01。然后使用add()方法将它们相加,得到了正确的结果0.06。 你可以根据需要使用BigDecimal类进行其他的数学运算,比如减法、乘法和除法。只需要调用相应的方法,如subtract()、multiply()和divide(),并传入相应的参数即可。 希望这个例子能够帮助你理解如何在Java中使用BigDecimal类进行精确的浮点数运算。 #### 引用[.reference_title] - *1* *2* *3* [java第二节课 java语法基础动手动脑](https://blog.csdn.net/weixin_30343157/article/details/102213401)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值