简体   繁体   中英

Catch Exception out side the Thread

I need to
I have tried to throw new exception but though it shows me error that "unreported exception...must be caught or declared to be thrown".2 if it's not possible then why? if you can explain reason.

here is my code

try {


            log("Connecting to Module..");
            int no = searchdevices.getSelectedIndex();
            String Selectaddress = searchdevices.getString(no);
            String name = Selectaddress.substring(0, 6);
            String add = Selectaddress.substring(Selectaddress.indexOf("$") + 1);
            if (no == -1) {
                Alert al = new Alert("Warning", "" + no, null, AlertType.WARNING);
                al.setTimeout(3000);
                display.setCurrent(al);
            }
            final String fdata2 = "btspp://" + add + ":1;master=false;encrypt=false;authenticate=false";
            finalurl = fdata2;
            fbtname = name;
            // fbtadd = add;
            new Thread(new Runnable() {

                public void run() {
                    try {

                        isConnOpen = true;
                        stream = (StreamConnection) Connector.open(fdata2);
                        in = stream.openInputStream();
                        out = stream.openOutputStream();
                        url2 = fdata2;

                        GoTo_Success();

                    } catch (IOException ex) {

                      throw new Exception();//in side exception 
                    }
                }
            }).start();
        } catch (Exception e) {
            log("Please switch on bluetooth and then try again"); // want to catch here..
        }

Thank You.

When you throw a new exception in catch you have to handle it surrounding it with try..catch.

Try this:

new Thread(new Runnable() {

             public void run() {
                 try {

                     isConnOpen = true;
                     stream = (StreamConnection) Connector.open(fdata2);
                     in = stream.openInputStream();
                     out = stream.openOutputStream();
                     url2 = fdata2;

                     GoTo_Success();

                 } catch (IOException ex) {

                   try {
                    throw new Exception();
                } catch (Exception e) {
                    e.printStackTrace();
                }//in side exception 
                 }
             }
         }).start();

Well given that a thread runs at the same time your code runs how could it catch the exception? Once you call start() the thread starts up (well, at some point after that call probably) and the rest of the program moves on past the catch.

Say, for example, the Thread is created and started in a method called "foo". Once you do start, the foo method reaches the end and returns to whatever called it. That then calls a method "bar". At that point in time the new Thread is actually scheduled to run, so the "bar" method is suspended and the run method in the Thread is executed. Now the exception happens. The program is far far far away from the catch you are trying to have happen. Even if it were not that part of the program is asleep.

The exception must be declared in the method declaration using a throws clause, only then you can use throw new Exception();

If you see Thread.run() then you will find that there is no throws Exception clause. Therefore you will get a compilation error.

See these links:

  1. http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Exceptions.html
  2. http://download.oracle.com/javase/tutorial/essential/exceptions/

If you want to get notified about exceptions in another thread, you'll have to manually transfer the exceptions.

One example would be to use a Queue<Exception> or something like this, and your worker threads would catch any exception that comes and add it to the queue.

The "main thread" (or a special exception handler thread, or the UI thread, or...) would then regularly poll the queue to see if there is any new exception, and if it is, it could show it to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM