1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。
import javax.swing.*;classAboutException {public static voidmain(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 instanceofArithmeticException)
System.out.println("被0除");else{
System.out.println(e.getMessage());
}
}finally{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}
把可能会发生错误的代码放进try语句块中。 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。 当异常发生时,程序控制流程由try语句块跳转到catch语句块。 不管是否有异常发生,finally语句块中的语句始终保证被执行。
2.阅读以下代码(CatchWho.java),写出程序运行结果:
public classCatchWho {public static voidmain(String[] args) {try{try{throw newArrayIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "/内层try-catch");
}throw newArithmeticException();
}catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
写出CatchWho2.java程序运行的结果:
public classCatchWho2 {public static voidmain(String[] args) {try{try{throw newArrayIndexOutOfBoundsException();
}catch(ArithmeticException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "/内层try-catch");
}throw newArithmeticException();
}catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
3.辨析:finally语句块一定会执行吗?
public classSystemExitAndFinally {public static voidmain(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语句不一定执行
4.如何跟踪异常的传播路径?
//UsingExceptions.java//Demonstrating the getMessage and printStackTrace//methods inherited into all exception classes.
public classPrintExceptionStack {public static voidmain( String args[] )
{try{
method1();
}catch( Exception e ) {
System.err.println( e.getMessage()+ "\n");
e.printStackTrace();
}
}public static void method1() throwsException
{
method2();
}public static void method2() throwsException
{
method3();
}public static void method3() throwsException
{throw new Exception( "Exception thrown in method3");
}
}
当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。
每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。
5.成绩:
importjava.util.Scanner;class judgeException extendsException{publicjudgeException(String s){super(s);
}
}class judgeException2 extendsException{publicjudgeException2(String s){super(s);
}
}public classTest {public static voidmain(String[] args) {boolean p=true;while(p){
System.out.println("请输入学生的分数:");
Scanner in=newScanner(System.in);
String s=in.next();try{for(int i=0;i57)){throw new judgeException("输入错误,请输入一个数字:");
}
}try{int m=Integer.parseInt(s);if(m<0||m>100){throw new judgeException2("请输入正确的分数");
}if(m<60){
System.out.println("不及格");
}else if(m>=60&&m<=69){
System.out.println("及格");
}else if(m>=70&&m<=79){
System.out.println("中");
}else if(m>=80&&m<=89){
System.out.println("良");
}else if (m>=90&&m<=100){
System.out.println("优");
}
}catch(judgeException2 e){
System.out.println(e.getMessage());
}
}catch(judgeException e){
System.out.println(e.getMessage());
}
}
}
}