2

Is it possible to postpone the execution of try-catch that is triggered on button click. I am tired of trying out various ways in doing this.None successful.

Lets say i have a button.On button click field change listener,these try catch statements are executed.

    ButtonField showInputButton = new ButtonField(" Search ",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
      showInputButton.setChangeListener(new FieldChangeListener() 
      {

            public void fieldChanged(Field field,int context) 
            {
                    Dialog.alert(TextField1.getText());
                //Here there are some code snippets for jumping to another form
                  try
                  {   
                    //Some statements that filter data from database
                  }
                  catch( Exception e ) 
                    {         
                        System.out.println( e.getMessage() );
                        e.printStackTrace();
                    }
            }
        }

                    );

      add(showInputButton);

Its not that i do not want these statements to be executed at all.But i rather want them to be executed after some actions previous to that try-catch block are encountered.

Is something of this sort possible.Please guide.

enter image description here

I firstly want to thank all of you who have responded their suggestions.I apologize for my inability to explain my question clearly regarding what exactly i was doing within those try catch statements and what makes me do a postpone of try-catch block. Please find the attached image to my exact requirement.

REEDITED

I have added the below image after enhancing the code recommended through suggestions and also to showcase what i have tried programmatically.Please communicate if the image is not clear.

enter image description here

learning_fly
  • 382
  • 1
  • 2
  • 11
  • So you want to wait for the user to close the dialog (`Dialog.alert`) before it does anything further? – weston May 17 '12 at 14:35
  • Why aren't they executed after the previous actions? – Jivings May 17 '12 at 14:35
  • @weston Nope.At a point before that try-catch is encountered,a new form(tab) is opened and that tab returns the results performed by the action processed within these try-catch statements.(I hope i have made it as clear as possible) – learning_fly May 17 '12 at 14:37
  • @Jivings Yes these try-catch statements sure are executed.But i just want to delay their execution. – learning_fly May 17 '12 at 14:39
  • 1
    Until what has happened? Just save the exception and rethrow it again when you actually want it. – Jivings May 17 '12 at 14:41
  • @Jivings As i have noted in my above comment to weston,i want my try catch to execute only after the tab it has jumped to has been closed or action performed there. – learning_fly May 17 '12 at 14:44
  • @Weston and Jivings please check the reedits. – learning_fly May 17 '12 at 15:24
  • I don't think `FieldChangeListener` is actually what you want on that button. – Jivings May 17 '12 at 15:26

2 Answers2

1

Well you can save the exception as a variable for later use...

Exception savedException;
try {   
   //Some statements that filter data from database
}
catch( Exception e ) {         
  savedException = e;
}

// do more stuff then deal with exception afterward
throw savedException;
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • Thanks for a response.Please find the attached image. – learning_fly May 17 '12 at 15:22
  • @learning_fly Shouldn't you be using a click listener instead of a FieldChangeListener then? – Jivings May 17 '12 at 15:24
  • This is being done into a blackberry JDE.Hence the FieldChangeListener keyword. – learning_fly May 17 '12 at 15:26
  • Not a mind reader. You should have that tag on your question. – Jivings May 17 '12 at 15:26
  • Except for partial syntactic differences theres not much difference in development.As Java is the native language being used there.I apologize though.What i am looking out now is the actual methodology that can be implemented here.Please ignore the syntax-wise correctness. – learning_fly May 17 '12 at 15:28
  • Except this is rather a specific programming question, which seems tied to the manner in which buttons work in Blackberry JDE. – Jivings May 17 '12 at 16:07
  • In your opinion is there any different way to implement this.Please suggest if you have any idea. – learning_fly May 17 '12 at 16:55
0

You could start a thread and wait for the condition you are looking for to happen (you might explicitly have to signal it to the thread when the consition is met)

Pseudocode:

static boolean isConditionMet = false;

public void fieldChanged(Field field,int context)  // inside the ButtonField
{
  Thread thread - new Thread() {
    public void run() {
      // TODO: wait for condition using isConditionMet
      // TODO: perform desired actions when condition happens
      // TODO: reset isConditionMet
    }
  };
  thread.start(); 
}

You of course will need to set isConditionMet somewhere lese where you can test/recognize that the condition you are looking for is true. You will also need to protect access to isConditionMet with some sort of Lock/Semaphore/Monitor to prevent simultaneous access.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Thanks for a response.Yeah I have tried that too.But not able to figure at which point should i run the thread and how.Could you suggest an example of an exact method. – learning_fly May 17 '12 at 14:34
  • Please find the attached image.How could I use the suggested code in this fashion. – learning_fly May 17 '12 at 15:23
  • 1
    Ok, you need no threads :) Your problem is that you implement a changeListener, which gets invoked at start. Instead implement the logic in a regular method and just call it from your other Pane when the button is clicked. You might also want to move the "jump to other Pane" statements at the end of the `try` block (or after the `catch` block), so you do the DB filtering before executing the code in the other Pane – Attila May 17 '12 at 15:29
  • I have tried enclosing the try-catch of pane 2 in a method and then called that method in pane 1.But that method just is'nt accessible there.That is the biggest problem i am facing implementing this.Is that what you intend to suggest as well. – learning_fly May 17 '12 at 15:35
  • 1
    Yes. You need to make that method public and you need to make the Pane2 object available to Pane1 (maybe create a `setOtherPane()` method and call it after both panes are created) – Attila May 17 '12 at 15:38
  • In the code in your question the jump happens before the try/catch block -- so you don't have the filtered data available in the other pane yet – Attila May 17 '12 at 15:43
  • Thank you so much for understanding my query and suggesting an effective way.I have tried making the pane 2 accessible to pane 1.But i am unsure whether it has indeed been set for invocation in pane 1.Please see the second image of what i tried.( I hope u find the text clear enough) – learning_fly May 17 '12 at 16:50
  • I meant to pass a reference of Pane2 to Pane1, not to modify the model (e.g. something along the lines of `pane1=new Pane(); pane2=new Pane(); pane1.setOtherPane(pane2);`) – Attila May 17 '12 at 16:58
  • I doubt whether theres any way of setting panes like that in BB. – learning_fly May 17 '12 at 17:01
  • I do not know BB, but I assume you have access to the Pane object somehow. – Attila May 17 '12 at 17:02
  • Its true that accessing pane over there is what is required.I wish u did know the manner of doing that too :)Infact i am able to create statement that jumps to Pane 2.Shouldn't that jump to the inheld implementation of Pane 2 as well. – learning_fly May 17 '12 at 17:06