捕获线程外部的异常

发布于 2024-11-04 02:17:43 字数 1590 浏览 1 评论 0原文

我需要捕获线程运行时发生的线程外部的异常。
我试图抛出新的异常,但它显示了错误“未报告的异常......必须被捕获或声明为抛出”。2 如果不可能那为什么? 如果你能解释一下原因。

这是我的代码

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..
        }

谢谢。

I need tocatch exception out side the Thread which is occurred while Thread is running.
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖伴 2024-11-11 02:17:43

假设线程在您的代码运行的同时运行,它如何捕获异常?一旦你调用 start() ,线程就会启动(好吧,可能是在调用之后的某个时刻),程序的其余部分将继续执行 catch 。

例如,线程是在名为“foo”的方法中创建并启动的。一旦开始, foo 方法就会到达末尾并返回到调用它的地方。然后调用一个方法“bar”。此时,新线程实际上已计划运行,因此“bar”方法被挂起,并执行线程中的 run 方法。现在异常发生了。这个程序离你想要发生的事情还很遥远。即使不是程序的那部分处于休眠状态。

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.

素衣风尘叹 2024-11-11 02:17:43

当你在 catch 中抛出一个新的异常时,你必须用 try..catch 来处理它。

试试这个:

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();

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();
找个人就嫁了吧 2024-11-11 02:17:43

异常必须在方法声明中使用 throws 子句进行声明,只有这样你才能使用 throw new Exception();

如果你看到 Thread.run() 那么你会发现没有throws Exception子句。因此你会得到一个编译错误。

请参阅以下链接:

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

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/
恏ㄋ傷疤忘ㄋ疼 2024-11-11 02:17:43

如果您想收到有关另一个线程中的异常的通知,则必须手动传输异常。

一个示例是使用 Queue或类似的东西,并且您的工作线程将捕获出现的任何异常并将其添加到队列中。

然后,“主线程”(或特殊的异常处理程序线程,或 UI 线程,或...)将定期轮询队列以查看是否有任何新的异常,如果有,它可以将其显示给用户。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文