Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Catch exceptions triggered by enterEventDispatcher

Hi!

 

Im throwing exceptions from code that was triggered by the event dispatcher.. but even though I sourround the call

 

try {

app.enterEventDispatcher();

}

catch (Throwable e) {

System.exit(0);

}

 

in my main method it will will not be catched and the application will keep on running just like nothing happened.

What is the prefered way to handle this?

 

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Catch exceptions triggered by enterEventDispatcher

"Im throwing exceptions from code that was triggered by the event dispatcher"

 

Which code is this?

 

Entering the Event Dispatcher does not actually, in my knowledge, run any of your code.  All it does is start the Event Thread for your Application - in fact as I understand it the Thread running 'main' is converted to your Event Thread.  So I don't think you can throw exceptions in code in the way you have described.  Which might explain why try catch is never triggered.  The Thread never returns from the enter Dispatcher call. 

Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Re: Catch exceptions triggered by enterEventDispatcher

Hi!

 

I will try to be more specific. Im running the helloworld you get from the eclipseplugin. In HelloBlackBerryScreen there is a makeMenu method in which there is some custom code and from here I throw the exception at the last row.

 

    protected void makeMenu( Menu menu, int instance ) {
        super.makeMenu(menu, instance);
        MenuItem mntmSayHello = new NewMenuItem();
        menu.add( mntmSayHello );
        throw new RuntimeException("!!!!!!!!!!!!!");
    }

 

I want to catch this exception and when I look at the stacktrace it will tell me that the call originates from the main method:

main -> enterEventDispatcher -> processNextMessage

 

so I try to catch the exception in the main method like:

 

  public static void main( String[] args ) {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        try {
            HelloBlackBerry theApp = new HelloBlackBerry();
            theApp.enterEventDispatcher();
        }
        catch (Throwable t) {
            System.out.println("!!!!!!!!!!!!!!!!");
        }
    }

 

But the Exception will not be caught? Does this mean I cant catch any exceptions from my GUI code?

 

/Br Johannes

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Catch exceptions triggered by enterEventDispatcher

What you see is correct.  The 'main method Thread is turned into the Event Thread, so its Stack Trace will show you that. 

 

If you look through the stack trace for makeMenu, I think you will find there are a variety of places that you catch this if you had code in there, for example, I believe that onMenu is called.  Not forgetting the initial event processing that started the menu creation rolling - presumably a key Event using the Menu key. 

 

But to me what you are doing makes no sense:  If you want to protect against errors in makeMenu, put a try/catch in there. 

 

So what are you actually trying to achieve?

Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Re: Catch exceptions triggered by enterEventDispatcher

Hi and thx for the fast answer!

 

Usually in java-environments, if an exception is not cought, then the application will exit, so that you will know that you have an error (along with stacktrace).

 

But in this UI-environment the application just keeps on running and errors get unnoticed. I want to find these errors and I could have a try/catch in the makeMenu but then I need to add this boilerplate code to all my UI-classes which is ugly and might be forgotten.

 

Do you know any best practise for how to handle this on the blackberry?

 

/Br Johannes

Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Re: Catch exceptions triggered by enterEventDispatcher

But I think you are right.. I could catch the exception by overrinding keyCharUnhandled in the class that extends MainScreen. Hopefully I can do the same to handle other gui-code exceptions. I would rather have one place to catch the exceptions but this will still be ok if it works in all scenarios. I will take a look at more stacktraces.

 

/Br Johannes

Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Catch exceptions triggered by enterEventDispatcher

"Usually in java-environments, if an exception is not caught, then the application will exit"

 

Not sure I agree with this.  The Thread will end, not the Application.  Or the exception will be caught by something in the Thread, not by something in the Application.  Remember you are running a multi-threaded, event driven application here, not a single Threaded Java program. 

 

"environment the application just keeps on running and errors get unnoticed."

 

This is because the Event Thread has its own try catch - it can't die, that would completely kill the Application in terms of user responsiveness. 

 

"Do you know any best practice for how to handle this on the blackberry?"

 

I don't know what you are trying to achieve.  You can't have a single 'catch' point for the entire application, that unfortunately makes no sense in the architecture that the BlackBerry uses.  And personally I think this is a dangerous practice anyway. 

 

"I could catch the exception by overriding keyCharUnhandled "

 

Perhaps, but why are you throwing a RuntimeException in makeMenu?

Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Re: Catch exceptions triggered by enterEventDispatcher

I tried to catch the exception thrown from sayHello in HelloBlackBerryScreen: 

 

private void sayHello() {
        Dialog.inform( "Hello " + basicEditField.getText() );
        throw new RuntimeException("sdfsdf");
    }

 

And Im having problems catching it.. according to the stacktrace I should be able to catch it in:

 

main -> HelloBlackBerry.enterEventDispatcher -> HelloBlackBerryScreen.touchEvent -> HelloBlackBerryScreen.fieldChanged

 

But the only part I can actually catch the exception is in HelloBlackBerryScreen.fieldChanged . This is very near the code and I would rather catch it in main or HelloBlackBerry.enterEventDispatcher to get rid of all the boilerplate code.

Contributor
Posts: 10
Registered: ‎05-21-2011
My Device: Torch
My Carrier: Sweden

Re: Catch exceptions triggered by enterEventDispatcher

Hi! Thx for the answer!

 

   "Usually in java-environments, if an exception is not caught, then the application will exit"

 

   "Not sure I agree with this.  The Thread will end, not the Application.  Or the exception will be caught by something in the Thread, not by something in the Application.  Remember you are running a multi-threaded, event driven application here, not a single Threaded Java program."

 

Yes, sorry for writing that wrong, the thread would of course be killed Smiley Happy

 

  "environment the application just keeps on running and errors get unnoticed."

  "This is because the Event Thread has its own try catch - it can't die, that would completely kill the Application in terms of user responsiveness. "

 

Yes this would kill the application (because its the main thread).. but that is how it should work. If you have an error in your application and cannot recover then the application should terminate. This is how it works in Android as far as I know.

 

  "Do you know any best practice for how to handle this on the blackberry?"

  I don't know what you are trying to achieve.  You can't have a single 'catch' point for the entire application, that  unfortunately makes no sense in the architecture that the BlackBerry uses.  And personally I think this is a dangerous practice anyway. 

 

I would like to have a single catch point for each thread in case the exception was not caught earlier. If not it would mean that if you have bugs in your UI code they will be hard to identify. Exceptions should never be magically catched because that is very dangerous.

 

  "I could catch the exception by overriding keyCharUnhandled "

 

  "Perhaps, but why are you throwing a RuntimeException in makeMenu?"

 

Im throwing it just because I want to test how Exception handling works in Blackberry.

 

/Br Johannes



 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Catch exceptions triggered by enterEventDispatcher

BlackBerry is known to consume Exceptions. If your app is designed around expecting Exceptions to be thrown/caught, you may want to redesign it.