异常处理学习例子

本文深入探讨Java中的异常处理机制,包括JVM如何使用方法调用堆栈来跟踪异常,并详细解释了try/catch结构的工作原理。同时提供了具体的代码示例说明如何定义和处理自定义异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=18] [b]异常处理学习例子[/b]
异常处理机制是JVM的方法调用堆栈.
JVM用方法调用栈来跟踪每个线程中一系列的方法调用过程.该栈保存了每个调用方法的本地信息.对于独立的JAVA程序,可以一直回溯到该程序的入口方法main().当一个方法被调用时,JVM把描述该方法的栈结构置入栈顶,位于栈顶的方法为正在执行的方法.

当一个JAVA方法正常执行完毕,JVM会从调用栈中弹出该方法的栈结构,然后继续处理前一个方法.如果JAVA方法在执行代码过程中抛出异常,JVM必须找到能捕获该异常的CATCH代码块.它首先查看当前方法是否存在这样的CATCH代码块,如果存在,就执行该CATCH代码块;否则,JVM会从调用栈中弹出该方法的栈结构,继续到前一个方法中查找合适的CATCH代码块,一直回溯到入口方法main().最后,如果JVM向上追溯到main()方法,仍然没有找到处理该异常的代码块,该线程就会异常终止.如果该线程是主线程,应用程序也将随之终止,此时JVM将把异常直接抛给用户,在用户终端上会看到原始的异常信息.

在上述回溯过程中,如果JVM在某个方法中找了处理该异常的代码块,则该方法的栈结构将成为栈顶元素,程序流程将转到该方法的异常处理代码部分继续执行.

注 : 在try/catch结构中,在try中抛出的异常,才是catch捕获的异常,catch捕获的异常不能捕获try中没有抛出的异常.

   如:
    try {
test.display(); //其抛出NotFoundException异常
} catch (NotFoundException e) {
       //此处catch只能捕获NotFoundException,如捕获IoException异
//常,则此处代码块将报错.
test.disNotFoundException();
NotFoundException.disNotFoundException();
}


package com.test;

import java.util.ArrayList;

/**
* 异常处理的练习例子
*
* @author huguangxiong
*
*/
public class TestEception {

ArrayList<Me> result = new ArrayList<Me>();

/**
* @param args
*/
public static void main(String[] args) {

TestEception test = new TestEception();

Me me1 = new Me();
Me me2 = new Me();
me1.setMess("aaaa");
me2.setMess("dddd");

test.add(me1);
test.add(null);
test.add(me2);


try {
test.display();
} catch (NotFoundException e) {
test.disNotFoundException();
NotFoundException.disNotFoundException();
// throw new ErrorFoundException("用disNotFoundException()方法处理的NotFoundExceptionNotFoundException");
e.printStackTrace();
} catch (ErrorFoundException e) {
System.out.println("===========================");
e.printStackTrace();
}



}

// catch (NotFoundException e) {
// // test.disNotFoundException();
// NotFoundException.disNotFoundException();
// // throw new
// // ErrorFoundException("用disNotFoundException()方法处理的NotFoundExceptionNotFoundException");
// }

public void add(Me me) {
if (me == null) {
System.out.println("Me对象为NULL,不能添加Me对象");
result.add(null);
return;
}
result.add(me);
}

public Me get() throws NotFoundException {
Me me = result.get(0);
if (me == null)
throw new NotFoundException("在result集合中没有Me对象");

return me;
}

public void display() throws NotFoundException,ErrorFoundException{
Me me = null;
Me me2 = null;
// try {
me = this.get();
if (me2 == null)
throw new ErrorFoundException("不能通过get()方法获得Me对象");

System.out.println("display中Me对象的属性是: " + me.getMess());
// } catch (NotFoundException ex) {
// this.disNotFoundException();
// throw new
// ErrorFoundException("用disNotFoundException()方法处理的NotFoundExceptionNotFoundException");
// }

}

public void disNotFoundException() {
Me me1 = new Me();
me1.setMess("bbbb");
result.add(me1);
System.out.println("添加Me1对象成功");
}

}

class Me {
private String mess;

public String getMess() {
return mess;
}

public void setMess(String mess) {
this.mess = mess;
}

public void display() {
System.out.println("mess of Me is : " + mess);
}
}

class NotFoundException extends Exception {

public NotFoundException() {
super();
}

public NotFoundException(String msg) {
super(msg);
}

public static void disNotFoundException() {
Me me1 = new Me();
me1.setMess("bbbb");
System.out.println("添加Me1对象成功");
}

}

class ErrorFoundException extends Exception {

public ErrorFoundException() {
super();
}

public ErrorFoundException(String msg) {
super(msg);
}
}
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值