• Post Reply Bookmark Topic Watch Topic
  • New Topic

Resume execution after exception  RSS feed

 
Raul Cosio
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Suppose I have a program that for some reason throws an exception, and, below the stack, there is some code that can catch the exception and solve the problem (I could say: re-open a JDBC connection, remove unused objects or something like that, think generic). Is it possible to resume execution to the point where the exception was thrown?, or, in code, say we have the following program:


Regards
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post. You can see how much better it looks
you should be able to resume execution after catching an Exception, yes. Here is one way you could do it, and there are lots of other ways:
  • Put the code into a loop.
  • If there is an Exception, use a boolean flag to show whether you should repeat the loop.
  • Your flag depends on whether you sorted out the problem or not.
  •  
    William P O'Sullivan
    Ranch Hand
    Posts: 859
    Chrome IBM DB2 Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, in the catch() you can do whatever you want to handle the exception.

    If you question is to continue say in subproc1() imeediately following where the exception was thrown,
    that would be a complex undertaking.

    WP
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The key thing is to handle the exception at the right level.

    You can't (easily) catch the exception at a high level and then jump back to where it started. Processing can only continue in the catch block or in the code immediately following it. So if you want to continue in subproc2(), you need to catch and handle the exception in subproc2().
     
    Campbell Ritchie
    Marshal
    Posts: 56227
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If it is something like reading lines, you could record the count of lines read (I think there is a class in java.io which reads and counts simultaneously), and continue where you left off. If you have a packet via a socket, it might be necessary to ask for it to be re‑sent.
     
    Raul Cosio
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you guys for your responses, that's what I'm meaning: Continue execution immediately where the exception was thrown.

    I'm just wondering if this could be possible, actually I don't have a problem that can't be solved with alternative ways. I was thinking about this when I was debugging my program and after an exception was thrown and catched, I wanted to have available all the variables of the the method in order to easily diagnose the problem, instead of having to stop the program, put a breakpoint and debug again (not to mention that the method could be executed many times before the problem appears).

    But, returning to the main discussion, I think that this is more related to changing the execution order of a java program, because it would be somethink like executing a JMP instruction in assembly language (rembember those old days?) without changing the stack.

    I was looking at the StackTraceElement class but it seems to be more informative than functional, so, I'm just wondering if there is some "magic" code or library in the latest versions of java that could do this. I don't know how java executes code, but it seems that when an Exception is catched, the information in the stack is already lost, except for the StackTraceElement's that were created with the Exception object...
     
    Campbell Ritchie
    Marshal
    Posts: 56227
    171
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not certain, but I suspect that the stack trace elements do not encapsulate everything which was on the stack (look here). It is worth having a look at the Java Language Specification (JLS), too; try the bit about runtime handling, and see what it says there. Also search the JLS index for “abrupt completion”.
     
    Paul Clapham
    Sheriff
    Posts: 22721
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You do it like this:



    At this point, if an exception was thrown, you know that it was thrown by that "one line of code". If you want to proceed with the next code, then do so. If not, then don't. Maybe rethrow the exception from the catch block, or set a flag in the catch block which you can interrogate with an if-statement.

    But no, once control has escaped from that code locality, you have no way of going back to it. There's no "go to" in Java.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!