Java课后练习9(异常处理)

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

}
}

输出结果:

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

 输出结果:

动手动脑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");

}

}

}

输出结果:

动手动脑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");

}
}
}

输出结果:

动手动脑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() + " " );
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" );
}
}

输出截图:

动手动脑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();
}
}

输出截图:

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

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,
"您的输入有误,请重新输入!");
}
}

}
}

输出截图:

原文地址:https://www.cnblogs.com/bai123/p/6103088.html