java异常处理运行的截图_java异常处理动手动脑

实验任务一、多层的异常捕获-1

1.实验内容:阅读以下代码(CatchWho.java),写出程序运行结果:

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.结果截图:

c57f4fe7f70f0e216e879d0aad0d631d.png

实验任务二、多层的异常捕获-2

1.实验内容:写出CatchWho2.java程序的运行结果

public class CathchWho2{

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

}

}

}

2.结果截图:

4d7af3193496d06244d0dcb16271c2ea.png

实验任务三:动手动脑1

1.实验内容:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

2.代码:

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

}

}

}

3.运行结果截图:

15288bcfd8a5817e390735859ea23a0a.png

4.总结:将可能要出现错误的语句放到try语句中,当程序检测到这个错误时,用catch语句捕获并且处理该错误,如果发生了异常,程序会由try语句跳到catch语句。但如果没有提供合适的异常处理代码,JVM会结束整个应用程序。

实验任务四:动手动脑2

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

代码:

import javax.swing.*;

public classEmbedFinally{

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

}

}

}

2.运行结果截图:

26b2fccfbe658a14e5bdc55861cfa916.png

3.总结:当try....catch....finally语句嵌套使用时,程序会先执行内层的try语句,当检测到错误时由catch语句捕获,而内层捕获并处理了异常,外层就不会再捕获,只执行finally语句块,如果内层并未处理该异常,则外层捕获并处理异常,执行外层的finally语句,比如将上述代码修改后的结果并不相同:

import javax.swing.*;

public classEmbedFinally{

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

}

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

}

}

}

输出结果为:

31610d2566d37f8a576a04fafda8bfb4.png

这是因为最内层的并没有catch语句捕获到异常,但仍执行finally语句块,由外层的catch语句捕获到异常并处理,执行该层的finally语句,再外层的catch便不再捕获该异常,只执行finally语句块。

实验任务五:动手动脑3

1.实验内容:辨析finally语句块一定会执行吗?请通过 SystemExitAndFinally.java示例程序回答上述问题

2.代码:

import javax.swing.*;

public class SystemExitAndFinally{

public static void main(String[] args)

{

try{

System.out.println("in main");

throw new Exception("Exception is thrown in main");

//System.exit(0);

}

catch(Exception e)

{

System.out.println(e.getMessage());

System.exit(0);

}

finally

{

System.out.println("in finally");

}

}

}

3.结果截图:

efee7dfb16c910f9dc10a44c28a31c89.png

4.分析:

如果上述代码修改为:

import javax.swing.*;

public class SystemExitAndFinally{

public static void main(String[] args)

{

try{

System.out.println("in main");

throw new Exception("Exception is thrown in main");

//System.exit(0);

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

finally

{

System.out.println("in finally");

}

}

}

而这段程序执行的结果为:

efe64858403be1218f3e84d85382c6a9.png

说明这段程序执行了finally语句块,一般情况下,finally语句块一定执行,但是当异常处理代码catch中执行System.exit(0)时,会退出java虚拟机,也就不再执行finally语句块。

实验任务六:归纳与总结

1.实验内容:依据对本讲多个示例程序的分析,请自行归纳总结出Java多层嵌套异常处理的基本流程。

2.总结:先内层捕获异常,进行处理,处理完之后外层不再处理,如有finally语句则执行,若catch语句中执行了System.exit(0)时,finally语句也不再执行。如果内层未捕获异常,则由外层处理异常,然后执行finally语句块,同样,若catch语句执行了System.exit(0),finally语句不再执行。如果有多个catch语句,则如果有一个捕获到之后,其它的catch语句被忽略,不再执行。

实验任务七:动手动脑4

1.实验内容:编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

2.代码:

package Tutorial9;

import java.util.*;

class yichang extends Exception{

}

class yichang1 extends Exception{

}

public class Zuoye {

static Zuoye A=new Zuoye();

public static void Dongnao(){

Scanner in=new Scanner(System.in);

System.out.print("请输入一个整数:");

int t=1;

int m1;

String m=in.next();

for(int i=0;i

if(m.charAt(i)'9')

{

try{

throw new yichang();

}catch(yichang e){

t=0;

System.out.println("数字格式异常,重新输入");

break;

}

}

}

if(t==1){

m1=Integer.parseInt(m);

if(m1>100){

try{

throw new yichang1();

}catch(yichang1 e){

System.out.println("不合法,重新输入");

}

}

else{

if(m1<60){

System.out.println("不及格");

}

else if(m1>=60)

{

if(m1>65&&m1<75){

System.out.println("中");

}

else if(m1>=75&&m1<90){

System.out.println("良");

}

else if(m1>=90&&m1<=100){

System.out.println("优");

}

else{

System.out.println("及格");

}

}

}

}

else

A.Dongnao();

}

public static void main(String args[]){

while(true){

A.Dongnao();

}

}

}

3.结果截图:

8a36b402c1d8e1fc34a05551f342a195.png

  • 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、付费专栏及课程。

余额充值