Results 1 to 6 of 6
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Pattern for handling multiple exceptions during a recursive operation

    I have a bunch of objects with a tree-like relationship. If the root of the tree is deleted, I must recursively detach all its branches. This involves calling a cleanup method on each object. The method does not throw any checked exceptions. However, I want to ensure that no RuntimeExceptions interfere with calling the cleanup method on all the detached objects. (The objects are user-supplied plugins; the code in them can't be vetted against RuntimeExceptions. The tree describes their dependency relationships.)

    What pattern would you use in this situtation? My idea is to catch any RuntimeExceptions and forward them to some listener to be logged and/or displayed.
    Get in the habit of using standard Java naming conventions!

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Pattern for handling multiple exceptions during a recursive operation

    Have you started this project or are you coming up with a plan before you start coding?
    My API:
    Java Code:
    cat > a.out || cat > main.class

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Pattern for handling multiple exceptions during a recursive operation

    The project is well underway. The plugin loader already basically works. I'm just thinking about how to handle inter-dependencies between plugins. I decided that I wanted to make it automatically unload plugins that requested the plugin being unloaded, and those that were only requested by the plugin being unloaded, and that's when I ran into the problem of needing to complete the recursion even if something throws an exception.
    Get in the habit of using standard Java naming conventions!

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Pattern for handling multiple exceptions during a recursive operation

    What exception(s) is/are thrown. These exceptions that pop up are causing your program's uploading process to freeze? Do these plugins upload themselves as individual entities that must do their uploading themselves or does a master overseer program do the uploading?

    My solution would be to do what the linux OS has done. Do both. When you run into an exception and a program throws an exception that cannot be bypassed, you have an overseer program finish the job of the uploading process for that plugin. Think of it as sending a plugin a "close/exit" signal and if that does not work, the program must end the plugin it's self. Just like the processes on a linux machine.

    If I am way off, make sure to tell me.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Pattern for handling multiple exceptions during a recursive operation

    There is no uploading involved. I don't know what exceptions might be thrown, since the plugins are user-supplied. As noted, the method being called on them is not declared to throw any checked exceptions. I'm just trying to protect the program against rogue RuntimeExceptions. But if I catch one, I can't re-throw it without disrupting the recursive unloading process.
    Get in the habit of using standard Java naming conventions!

  6. #6
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Pattern for handling multiple exceptions during a recursive operation

    OH! now I see what you are trying to do. I think. Well, my suggestion would be to catch all exceptions. Since you will have no idea what will be thrown, you will have to catch Throwable. By catching Throwable, you will catch every exception since every exception is an extenstion of Throwable.

    Java Code:
    try {
    	throw new ArrayIndexOutOfBoundsException();
    } catch (Throwable t) {
    t.printStackTrace();
    //some code to store the StackTrace
    System.out.println("Proceeding with recursion");
    }
    System.out.println("Uploading!!");
    This should protect the program from RuntimeExceptions, as long as it is placed in the right place.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. Handling multiple WAV files and saving them into ONE
    By phil128 in forum Advanced Java
    Replies: 2
    Last Post: 12-11-2010, 02:31 PM
  2. Multiple Exceptions with Array
    By Cutter in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 07:02 PM
  3. Replies: 24
    Last Post: 12-03-2009, 10:58 PM
  4. Recursive Star Pattern HELP PLEASE!!
    By ITnewbie in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 09:55 PM
  5. TCPIP Client (handling specific exceptions)
    By ravian in forum Networking
    Replies: 3
    Last Post: 11-29-2007, 09:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •