• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Resume execution after exception

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Ranch Hand
    Posts: 859
    IBM DB2 Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 76888
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 76888
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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”.
     
    Sheriff
    Posts: 27465
    88
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Did you miss me? Did you miss this tiny ad?
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic