---
title: "Dynamic Proxy的异常处理"
author: "cherami"
date: "2008-08-11T13:08:13Z"
tags: []
slug: "dynamic-proxy"
draft: false
lastmod: "2008-08-11T13:08:13Z"
id: 230100
url: "https://cherami.iteye.com/blog/230100"
---

现在越来越感觉Dynamic Proxy是个好东西，不过发现异常处理还是得注意下，因为调用实际的代码返回的异常都会被包一次成为InvocationTargetException，所以如果你想让原本的异常继续往上抛，那么必须捕获异常并且得到targetException然后再抛出去：         try {             result = targetMethod.invoke(targetObject, args);         } catch (IllegalAccessException e) {             throw new RuntimeException("IllegalAccessException", e);         } catch (IllegalArgumentException e) {             throw new RuntimeException("IllegalArgumentException", e);         } catch (InvocationTargetException e) {             throw e.getTargetException();         }