有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

squeak Java如何在新线程上打开异常,而不是抛出异常的线程

我希望获取异常堆栈以在新线程上打开调试器,同时取消阻止引发原始异常的线程。在Java中可能吗

在Squeak中,此方法起作用:

StandardToolSet>>#debugException: anException
        "For convenience. Construct a helper process to debug an exception 
that occurred in the active process later on so that the active process 
can (try to) resume. Uses a temporary variable to access and copy the 
signaler context now before it gets GC'ed."
    
    | helperProcess |
    helperProcess := (Process
        forContext: anException signalerContext copyStack
        priority: Processor activeProcess priority)
            shouldResumeFromDebugger: false;
            yourself.

    Project current addDeferredUIMessage: [
        helperProcess
            debugWithTitle: anException description
            full: false].
    ```

共 (2) 个答案

  1. # 1 楼答案

    因为你问的是Squeak/Smalltalk,我不确定你希望你的工具有多强大。特别是关于anException signalerContext copyStack中的堆栈复制,以及可能继续复制正在进行的计算,Java中的工作量可能要高得多

    要简单地查看堆栈帧,可以使用Throwable.getStackTrace。通过在某处用try { ... } catch (...) { ... }捕捉异常,可以获得异常(这是一个可丢弃的)。你也可以get the trace from the currently running Thread anywhere

    提取堆栈跟踪后,线程将在下一行正常继续。所以它是“未被阻止的”,但不是重复的。请注意,如果抛出了异常,并且您处于catch块中,则堆栈已经展开到该点,这与Squeak/Smalltalk不同,Squeak/Smalltalk将异常处理帧置于信令上下文之上。Java程序将在catch块之后继续(除非您再次抛出另一个或同一个异常)

    因此获得的堆栈跟踪没有处理Squeak/Smalltalk上下文那么强大。StackTraceElements允许您查看跟踪的某些方面,但不能控制它们

    对于任何调试器,比如单步执行和检查局部变量,都可以研究Java Platform Debugger Architecture (JPDA)的Java调试接口。另一种方法是研究Eclipse调试器的工作方式。但我无法告诉您这些是否允许您调试线程的副本

  2. # 2 楼答案

    如果可以的话,我会把标题改成In Java, how to handle exceptions without blocking?。答案似乎是one cannot do so. Not possible.

    所以,这个问题的答案是否定的。不可能