我有抛出异常的代码。在下面的代码行中,container.setIsDefault(true)抛出InvocationTargetException。我想知道异常的实际原因,所以我将此代码包装在try ..catch块中。 但是使用try catch块会给我编译时错误 “InvocationTargetException的无法访问的catch块。永远不会从try语句主体抛出此异常”
任何想法如何找出异常的实际原因。
try{
EdmEntityContainer.Builder container= EdmEntityContainer.newBuilder();
container.setName(dataBase);
container.setIsDefault(true);
container.setLazyLoadingEnabled(true);
container.addEntitySets(entitySets);
}catch (InvocationTargetException e)
{
Throwable cause = e.getCause();
System.out.format("Invocation of failed because of: %s%n", cause.getMessage());
}
更新: 我在代码中添加了RuntimeException和假的InvocationTargetException。但是它不会进入任何一个catch块。它直接转到目标为null的函数下面
public InvocationTargetException(Throwable target) {
super((Throwable)null); // Disallow initCause
this.target = target;
}
这是stacktrace,现在异常来自line container.addEntitySets(entitySets);
InvocationTargetException.<init>(Throwable) line: 72
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 606
JavaMethodInvokerFactory$1.invoke(Method, Object, Object...) line: 60
AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(Object, HttpContext) line: 205
AbstractResourceMethodDispatchProvider$ResponseOutInvoker(ResourceJavaMethodDispatcher).dispatch(Object, HttpContext) line: 75
HttpMethodRule.accept(CharSequence, Object, UriRuleContext) line: 288
ResourceClassRule.accept(CharSequence, Object, UriRuleContext) line: 108
RightHandPathRule.accept(CharSequence, Object, UriRuleContext) line: 147
RootResourceClassesRule.accept(CharSequence, Object, UriRuleContext) line: 84
WebApplicationImpl._handleRequest(WebApplicationContext, ContainerRequest) line: 1469
WebApplicationImpl._handleRequest(WebApplicationContext, ContainerRequest, ContainerResponse) line: 1400
WebApplicationImpl.handleRequest(ContainerRequest, ContainerResponse) line: 1349
WebApplicationImpl.handleRequest(ContainerRequest, ContainerResponseWriter) line: 1339
ServletContainer$InternalWebComponent(WebComponent).service(URI, URI, HttpServletRequest, HttpServletResponse) line: 416
ServletContainer.service(URI, URI, HttpServletRequest, HttpServletResponse) line: 537
ServletContainer.service(HttpServletRequest, HttpServletResponse) line: 708
ServletContainer(HttpServlet).service(ServletRequest, ServletResponse) line: 848
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 303
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 208
WsFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 52
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 241
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 208
StandardWrapperValve.invoke(Request, Response) line: 220
StandardContextValve.invoke(Request, Response) line: 122
Authenticator(AuthenticatorBase).invoke(Request, Response) line: 501
Authenticator.invoke(Request, Response) line: 197
StandardHostValve.invoke(Request, Response) line: 170
ErrorReportValve.invoke(Request, Response) line: 98
TenantValidationValve.invokeNextValve(Request, Response) line: 255
TenantValidationValve.invoke(Request, Response) line: 101
RequestTracingValve.invoke(Request, Response) line: 24
RequestTracingValve.invoke(Request, Response) line: 27
StandardEngineValve.invoke(Request, Response) line: 116
CoyoteAdapter.service(Request, Response) line: 408
Http11Processor(AbstractHttp11Processor<S>).process(SocketWrapper<S>) line: 1040
Http11Protocol$Http11ConnectionHandler(AbstractProtocol$AbstractConnectionHandler<S,P>).process(SocketWrapper<S>, SocketStatus) line: 607
JIoEndpoint$SocketProcessor.run() line: 315
ThreadPoolExecutor(ThreadPoolExecutor).runWorker(ThreadPoolExecutor$Worker) line: 1145
ThreadPoolExecutor$Worker.run() line: 615
TaskThread(Thread).run() line: 745 [local variables unavailable]
答案 0 :(得分:1)
这里最可能的问题是异常实际上不是TargetInvocationException,而是包装它的另一个异常。
将您的catch块更新为:
}catch (RuntimeException e)
看看会发生什么。