CantThinkOfAnything
CantThinkOfAnything

Reputation: 1179

try/catch not working in Java?

I am making an app for android in Qt. So in order to use the Google API I need to implement Java. So I have looked through the QtNotifier example and I am trying to implement the same as a starter.

This Java example comes from the QtNotifier example aswell so it should work the same but it doesn't. I have tried to debug it using the Qt debugger but the breakpoints do not seem to trigger So I added println statements to see at which line it goes wrong. But this is not enough so I am trying to print a stacktrace using a catch/try clause.

I have implemented it like this:

package org.qtproject.qt5.example;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import java.lang.Object;
import java.io.Writer;
import java.io.StringWriter;
import java.io.PrintWriter;


public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
{
    private static NotificationManager m_notificationManager;
    private static Notification.Builder m_builder;
    private static NotificationClient m_instance;

    public NotificationClient()
    {
        System.out.println("it works2222");
        m_instance = this;
    }

    public static void notify(String s)
    {
        System.out.println(s);

        try {
            if (m_notificationManager == null) {
                System.out.println("1111");
                m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
                System.out.println("2222");
                m_builder = new Notification.Builder(m_instance);
                System.out.println("3333");
                m_builder.setContentTitle("A message from Qt!");
                System.out.println("4444");
            }

            System.out.println("5555");
            m_builder.setContentText(s);
            System.out.println("6666");
            m_notificationManager.notify(1, m_builder.build());
            System.out.println("7777");

        } catch(Exception e) {
            StringWriter writer = new StringWriter();
            PrintWriter pw = new PrintWriter(writer);
            e.printStackTrace(pw);
            String errorDetail = writer.toString();
        }
    }
} 

The output is:

I/System.out( 4768): test string
I/System.out( 4768): 1111

It would seem that m_instance is still null because "System.out.println("it works2222");" does not get called. But the error does not get caught.

Is it because this is an error that is not an exception?

I have also tried running the QtNotifier app but the printstatement inside.

public NotificationClient()
{
    System.out.println("it works2222");
    m_instance = this;
}

But that is also not called in the QtNotifier app.

My question is: How can I trace this error?

Upvotes: 1

Views: 1639

Answers (1)

fdsa
fdsa

Reputation: 1409

In Java, there are both Errors and Exceptions. Normally, an Error is significant enough that your program should just crash and exit. However, for debugging purposes, if you want to catch both you should

catch (Throwable t)
{
    t.printStackTrace();
}

to get information on both.

Upvotes: 2

Related Questions