1 .阅读并运行下面程序,然后了解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");
}
}
}
运行截图:
使用Java异常处理机制:
把可能会发生错误的代码放进try语句块中。
当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。
catch语句块中的代码用于处理错误。
当异常发生时,程序控制流程由try语句块跳转到catch语句块。
不管是否有异常发生,finally语句块中的语句始终保证被执行。
如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。
2 .请尝试解释以下奇怪的现象:
用javap去反汇编两个示例程序的.class文件。第一个程序生成idiv字节码指令,第二个程序生成ddiv字节码指令。
JVM在具体实现这两个指令时,采用了不同的处理策略,导致两段代码运行时得到不同的结果
3 .阅读以下代码,写出程序运行结果:
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");
}
}
}
运行截图:
4 .写出程序运行的结果:
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");
}
}
}
运行截图:
5 .阅读示例,再运行它,观察其输出并进行总结。
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");
}
}
}
运行截图:
总结:
try...catch语句为配对出现,程序会按着代码的顺序依次执行,当try中语句出现异常时,他会执行与当前try配对的catch语句。
6 .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");
}
}
}
运行结果:
finally不会每次都执行,例如以上程序,当执行完throw new Exception("Exception is thrown in main");语句后便关闭了程序。
7 . 如何跟踪异常的传播路径?
当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。
可使用printStackTrace 和 getMessage方法了解异常发生的情况:
printStackTrace:打印方法调用堆栈。
每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。
示例程序:
//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" );
}
}
截图:
8 .以下代码完全符合Java语法规范,但是为什么它们不能通过编译:
public class TestThrows {
public static void main(String[] args) {
FileInputStream fis = new
FileInputStream("a.txt");
}
}
throws语句表明某方法中可能出现某种(或多种)异常,但它自己不能处理这些异常,而需要由调用者来处理。 当一个方法包含throws子句时,需要在调用此方法的代码中使用try/catch/finally进行捕获,或者是重新对其进行声明,否则编译时报错。
9 .编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
try
{
System.out.println("请输入成绩");
Scanner in=new Scanner(System.in);
double a=in.nextDouble();
if(a<60&&a>=0) System.out.println("不及格");
else if(a<70) System.out.println("及格");
else if(a<80) System.out.println("中");
else if(a<90) System.out.println("良");
else if(a>=90&&a<=100) System.out.println("优");
else System.out.println("成绩无效");
}
catch(Exception e)
{
System.out.println("成绩无效");
}
}
}