1. 请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识
(1)源程序
import javax.swing.*;
class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
k=i/j;//double可以被0除,i是正数时,结果是正无穷,i是负数时,结果是负无穷;
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"+k);
}
}
}
(2)j是int型的0,所以做除数就会出错,用异常处理解决这个问题,
实现的语句如下:
try{
//可能出现的运行错误的代码
}
catch(异常类型 异常对象引用){
//用于处理异常的代码
}
finaly{
//用于善后的代码
}
总结:
把可能会发生错误的代码放进try语句块中。当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。当异常发生时,程序控制流程由try语句块跳转到catch语句块。不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。
2.多层的异常捕获(阅读代码,写出程序运行的结果)
(1)
1)源代码
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)源程序:
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");
}
}
}
(2)运行结果
1)
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
调用第一个throw和catch还有第二个throw和catch,最后一个catch没有调用。
2)ArrayIndexOutOfBoundsException/外层try-catch
调用了try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
这两个,throw一个然后catch一个,还没有catch不会throw另一个。
3.finally请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结
(1)源程序
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");
}
}
}
(2)运行结果
(3)总结
当有多层嵌套的finally时,异常在不同层次抛出,在不同位置抛出,可能会导致不同的语句块执行顺序。
1)与finally对应的try和catch执行了,finally语句才会执行。
2)当finally前面有System.exit(0)执行时,finally也不会执行,程序会结束。
3)不管是否有异常发生,finally语句块中的语句始终保证被执行
4.finally语句块一定会执行吗?
(1)源程序
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");
}
}
}
(2)运行结果截图
3)解释
不一定会执行,在上述程序中,执行了try语句中的System.exit (0)语句,结束了程序,不会指向finally的语句了。
5.如何跟踪异常的传播路径
// 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" );
}
}
当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。
可使用printStackTrace 和 getMessage方法了解异常发生的情况:
printStackTrace:打印方法调用堆栈。
每个Throwable类的对象
都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。
6.验证
(1)子类抛出受控异常的限制
一个子类的throws子句抛出的异常,不能是其基类同名方法抛出的异常对象的父类。
(2) 抛出多个受控异常的方法
当一个方法声明抛出多个异常是,在此方法体中只要catch其中一个异常,程序就能顺利运行
7.
(1)源程序
import java.util.Scanner;
/*
* 用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
*/
//陈晓阳 2016.11.24
public class Score {
public static void main(String args[])
{
System.out.println("请输入学生的成绩");
int a=0;String str;
Scanner in2=new Scanner (System.in);
str=in2.next();
boolean flag=true;//flag用来表示是否输入合理,true表示合理,flag1表示是否是整数,false表示表示是整数
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<'0'||str.charAt(i)>'9')
{flag=false;}
}
if(flag==false)
try
{
throw new MyException3(str);
}
catch (MyException3 e)
{
System.out.println("输入的成绩不合理!请重新输入");
System.exit(0);
}
if(flag==true)
{
a=Integer.parseInt(str);
if(a<60)System.out.println("不合格!");
else if(a<70)System.out.println("合格!");
else if(a<78)System.out.println("中!");
else if(a<90)System.out.println("良!");
else System.out.println("优!");
}
}
}
class MyException3 extends Exception//输入的成绩不合理,不只是数字
{
MyException3(String a)
{
super();
}
}
(2)程序运行截图
(3)程序分析
首先定义一个异常处理类,继承Exception,主方法中调用异常处理,要判断是否是异常数据输入,数据输入后应该先转化成string类型的,接着转化成char数组,判断数组元素中是否有非数字的输入,有就调用异常处理,throw异常,并catch异常,输出“输入数据不合理”。合理的话就利用if语句判断数据范围,输出结果。