java try catch 块无法使用反射工作 - 不知道为什么,也可能是线程错误
在netbeans rcp中编写一个系统 - 不确定这是否重要,我只是不信任rcp
我们的系统正接近关键时刻,并且不断发生以下错误(有时它在10分钟后发生,有时它运行2天然后发生了这让我相信这可能是一个线程错误)
我们有一个实现了可运行的套接字读取器类 - 这是代码示例
@Override
public void run() {
while (!Thread.interrupted()) {
try {
/*
* can't display actual code
* reads data through socket and passes stream to a new handler class
* which uses reflection to create new object based off socket stream
* (none of this happens in a separate thread)
* (we're not holding a reference to the handler class - a new one is
* -created every iteration)
*
* at some point during the creation of this object, we get a socket
* closed exception happening at SocketInputStream.socketRead()
*/
} catch (Exception ex) {
cleanup();
}
}
}
我期望发生的是套接字异常应该被 catch 捕获堵塞并且执行了清理,
最终发生的情况是我得到了 java.net.SocketException 套接字关闭的可见堆栈跟踪(在 netbeans 未捕获的异常窗口中弹出并显示在控制台中) - 由于客户要求,我无法发布堆栈跟踪
我们有以下方法(这是实际抛出异常的方法)
public Abstract________ new________(Class<? extends Abstract________> clazz,
DataInput input) {
...
try {
// reflection code here
} catch (Exception ex) {
LOG.error("...");
throw new RuntimeException(ex);
}
...
}
还有什么奇怪的是,在实际的套接字输入处理程序类中,如果我尝试在异常读取的 catch 块之后手动抛出 java.net.SocketException, java.lang.RuntimeException: java.net.SocketException 异常:套接字已关闭
,但我们随机收到的堆栈跟踪只是简单地说 java.net.SocketException:套接字已关闭
这让我相信在我们的套接字流处理类中,它甚至没有捕获异常。
有人对为什么会发生这种情况有任何意见吗???
另外 - 我很抱歉我无法发布实际的代码,到目前为止我并不想神秘,
我想知道的一件事 - 我对反射有点熟悉,但在很大程度上不是 - 当我手动抛出异常,它总是被 try catch 块捕获 - 如果从反射中的某些内容抛出异常,是否有任何方法可以破坏 try catch 契约 - 我不明白为什么会这样,但我的意思是,我 现在不确定并抓住救命稻草
老实说,我 已获得发布已编辑堆栈跟踪的权限
在 MessageReader 类中没有单独的线程 - newMessage 方法是捕获异常,将其包装到 RuntimeException 中并抛出 - ____Reader 是带有 while 循环的套接字读取器,
这两种方法都显示在堆栈跟踪中,
不确定这是否有帮助
writing a system in netbeans rcp - not sure if this matters, i just don't trust the rcp
We're approaching crunch time on our system and the following error keeps happening (sometimes it happens after 10 minutes, sometimes it runs for 2 days and then happens which is leading me to believe it could be a threading error)
we have a socket reader class which implements runnable - here is a sample of the code
@Override
public void run() {
while (!Thread.interrupted()) {
try {
/*
* can't display actual code
* reads data through socket and passes stream to a new handler class
* which uses reflection to create new object based off socket stream
* (none of this happens in a separate thread)
* (we're not holding a reference to the handler class - a new one is
* -created every iteration)
*
* at some point during the creation of this object, we get a socket
* closed exception happening at SocketInputStream.socketRead()
*/
} catch (Exception ex) {
cleanup();
}
}
}
what i expect to happen is that the socket exception should just be caught by the catch block and the cleanup executed
what ends up happening instead is i get a visible stack trace (pops up in the netbeans uncaught exception window and displays in the console) for java.net.SocketException socket closed - i cannot post the stack trace due to customer requirements
what else is strange is that in the actual socket input handler class we have the following method (this is the method that the exception is actually being thrown from)
public Abstract________ new________(Class<? extends Abstract________> clazz,
DataInput input) {
...
try {
// reflection code here
} catch (Exception ex) {
LOG.error("...");
throw new RuntimeException(ex);
}
...
}
if i try manually throwing the java.net.SocketException after the catch block the exception reads java.lang.RuntimeException: java.net.SocketException exception: socket closed
yet the stack trace that we receive randomly simply says java.net.SocketException: socket closed
this is leading me to believe that in our socket stream handling class it's not even catching the exception to begin with.
Does anybody have any input as to why this may be happening???
also - i am very sorry that i can't post the actual code, i'm not trying to be cryptic
so far the one thing i'm wondering about - i am somewhat familiar with reflection but not to a great extent - when i manually throw an exception it always gets caught by the try catch block - if an exception is thrown from something in reflection is there any way it could break the try catch contract - i don't see why it would but i mean, i'm honestly not sure and grasping for straws at this point
i have received permission to post an edited stack trace
in the MessageReader class there is no separate thread - the newMessage method is where the exception is being caught, wrapped into a RuntimeException and thrown up - the ____Reader is the socket reader with the while loop
both methods do show up in the stack trace
not sure if this will help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抛出的错误 (java.lang.Error) 可能会绕过您的 catch 块(仅捕获异常)。尝试捕获错误并打印出您需要的任何调试语句。不要忘记从 catch 块中重新抛出错误。
也可能是在 catch 块中抛出异常。
A thrown Error (java.lang.Error) may be bypassing your catch block (which is only catching Exceptions). Try catching Error and print out whatever debug statements you need. Don't forget to rethrow the error from the catch block.
It could also be that an Exception is being thrown in the catch block.
我想到了两种可能性:
Two possibilities come to mind: