JAVA实验报告九异常处理_Java课后练习9(异常处理)

这篇博客通过多个动手动脑的实例,详细介绍了Java异常处理的机制和应用场景,包括try-catch-finally语句的使用,异常的传播路径,以及在实际编程中如何确保程序的健壮性。读者可以通过这些例子深入理解Java异常处理的重要性及其工作原理。
摘要由CSDN通过智能技术生成

动手动脑1:

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

}

}

}

输出结果:

714745b6a26df88f0aaa0a3965e0da94.png

动手动脑2:

代码:

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

}

}

输出结果:

c825cf4f12b9ba57e3731b5c6f918297.png

动手动脑3;

源代码:

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

}

}

}

输出结果:

bb5627db44f5bfd40fa96e5b7150a059.png

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

源代码:

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

}

}

}

输出结果:

9ff895e05d07398a8d01b941efe43175.png

动手动脑5:

finally语句块一定会执行吗?

至少有两种情况下finally语句是不会被执行的:

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

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

源代码:

// UsingExceptions.java

// Demonstrating the getMessage and printStackTrace

// methods inherited into all exception classes.

public class PrintExceptionStack {

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

}

}

输出截图:

b7eb8721eb875e0ed11416f1a729a988.png

动手动脑6:

如何跟踪异常的传播路径?

当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

可使用printStackTrace 和 getMessage方法了解异常发生的情况: printStackTrace:打印方法调用堆栈。

每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

源代码:

import java.io.*;

public class ThrowMultiExceptionsDemo {

public static void main(String[] args)

{

try {

throwsTest();

}

catch(IOException e) {

System.out.println("捕捉异常");

}

}

private static void throwsTest() throws ArithmeticException,IOException {

System.out.println("这只是一个测试");

// 程序处理过程假设发生异常

throw new IOException();

//throw new ArithmeticException();

}

}

输出截图:

c3c0550902c8f9e5382bc9d076722d93.png

一个方法可以声明抛出多个异常

int g(float h) throws OneException,TwoException { …… }

这只是一个测试

捕捉异常

import java.io.*;

public class Test

{

public void test()throws IOException

{

FileInputStream fis = new FileInputStream("a.txt");

}

}

class Sub extends Test

{

//如果test方法声明抛出了比父类方法更大的异常,比如Exception

//则代码将无法编译……

public void test() throws FileNotFoundException

{

//...

}

}

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

动手动脑7:

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

要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

源代码:

import javax.swing.JOptionPane;

public class Score {

public static void main(String[] args){

for(;;)

{

String a = JOptionPane.showInputDialog("请输入一个成绩:");

try{

int b = Integer.parseInt(a);

if(b>0&&b<60){

JOptionPane.showMessageDialog(null,

"不及格!");

break;

}

else if(b>=60&&b<=70)

{

JOptionPane.showMessageDialog(null,

"及格!");

break;

}

else if(b>70&&b<=80)

{

JOptionPane.showMessageDialog(null,

"成绩中等!");

break;

}

else if(b>80&&b<=90)

{

JOptionPane.showMessageDialog(null,

"成绩良好!");

break;

}

else if(b>90&&b<=100)

{

JOptionPane.showMessageDialog(null,

"成绩优秀!");

break;

}

else if(b>100||b<0)

{

JOptionPane.showMessageDialog(null,

"您输入的成绩超出范围,请重新输入!");

}

}

catch(Exception e){

JOptionPane.showMessageDialog(null,

"您的输入有误,请重新输入!");

}

}

}

}

输出截图:

685d29918087054f61c5ff4e8f03eb9a.png

2dfc89d4a490dbccf56f0351f1d662e0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值