首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → 异常回调

异常回调

摘要: 异常回调

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"><table width="667" height="24" border="0"> <tr> <td width="394" valign="top">
java程序发生异常时,我们一般用try,catch块来处理,但我们也可以实现自己的异常回调机制把发生的异常(包括RuntimeException)通知到我们处理异常的对象。

首先定义我们自己的处理异常的类(异常临听器),它实现ExceptionListener接口。

public interface ExceptionListener {
public void exceptionOccurred(Exception x, Object source);
}

第一个参数是发生的异常的引用,第二个参数是捕获该异常的类的引用。

如下是一个实现类: </td> <td width="263">
</td></tr> </table> ??
public class ExceptionCallbackMain extends Object implements ExceptionListener {

private int exceptionCount;
public ExceptionCallbackMain() {
exceptionCount = 0;
}
public void exceptionOccurred(Exception x, Object source) {
exceptionCount++;
System.err.println("EXCEPTION #" + exceptionCount +", source=" + source);
x.printStackTrace();
}
public static void main(String[] args) {
ExceptionListener xListener = new ExceptionCallbackMain();
ExceptionCallback ec = new ExceptionCallback(xListener);
}
}

xListener对象将监视ec对象,一旦活动的ec对象内部发生异常,将触发exceptionOccurred(x,source)的调用来处理异常。请看ExceptionCallback.java源文件:
import java.io.*;
import java.util.*;

public class ExceptionCallback extends Object {
private Set exceptionListeners;//多个临听器的集合
private Thread internalThread;//内部线程,自运行对象,用来产生异常
private volatile boolean noStopRequested;

public ExceptionCallback(ExceptionListener[] initialGroup) {//构造函数
init(initialGroup);//有一组临听器
}

public ExceptionCallback(ExceptionListener initialListener) {//构造函数,添加一个临听器
ExceptionListener[] group = new ExceptionListener[1];
group[0] = initialListener;
init(group);
}

public ExceptionCallback() {
init(null);
}

private void init(ExceptionListener[] initialGroup) {
System.out.println("in constructor - initializing...");
exceptionListeners =Collections.synchronizedSet(new HashSet());
if ( initialGroup != null ) {
for ( int i = 0; i < initialGroup.length; i++ ) {
addExceptionListener(initialGroup[i]);
}
}

noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();//产生异常的代码
} catch ( Exception x ) {
sendException(x);//传速异常
}
}};
internalThread = new Thread(r);//内部线程
internalThread.start();
}

private void runWork() {
try {
makeConnection(); // will throw an IOException
} catch ( IOException x ) {
sendException(x);//传速异常
}
String str = null;
int len = determineLength(str); // NullPointerException
}

private void makeConnection() throws IOException {
String portStr = "j20";
int port = 0;
try {
port = Integer.parseInt(portStr);//产生一个异常
} catch ( NumberFormatException x ) {
sendException(x);//传速异常
port = 80;
}
connectToPort(port); // will throw an IOException
}

private void connectToPort(int portNum) throws IOException {
throw new IOException("connection refused");
}

private int determineLength(String s) {
return s.length();
}

public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}

public boolean isAlive() {
return internalThread.isAlive();
}

private void sendException(Exception x) {
if ( exceptionListeners.size() == 0 ) {//如果没有临听器
x.printStackTrace();
return;
}


synchronized ( exceptionListeners ) {
Iterator iter = exceptionListeners.iterator();
while ( iter.hasNext() ) {
ExceptionListener l =(ExceptionListener) iter.next();
l.exceptionOccurred(x, this);//处理异常
}
}
}

public void addExceptionListener(ExceptionListener l) {//添加一个临听器
if ( l != null ) {
exceptionListeners.add(l);
}
}

public void removeExceptionListener(ExceptionListener l) {//删除一个临听器
exceptionListeners.remove(l);
}

public String toString() {
return getClass().getName() +"[isAlive()=" + isAlive() + "]";
}
}
function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: String与StringBuffer
后一篇: 线程池与工作队列

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved