简体   繁体   English

如何防止异常停止我的程序?

[英]How do I prevent an exception from stopping my program?

I'm using a thread to run a half of an application, and the other half is running by itself. 我正在使用一个线程来运行一个应用程序的一半,而另一半正在单独运行。 I did this so that the one part fetching the data (because I'm running an infinite loop) would not stop the other part from doing it's job. 我这样做是为了使一部分获取数据(因为我正在运行无限循环)不会阻止另一部分完成其工作。 Now I was wondering: if an exception occurred on one part it would block the some of the other part because they are dependent on each other. 现在我在想:如果一个部分发生异常,它将阻止另一部分,因为它们彼此依赖。

So my question is, how could I prevent an exception from stopping my whole program and make it continue do things? 所以我的问题是,如何防止异常停止我的整个程序并使它继续运行?

Try-catch or throw an exception. 尝试捕获或引发异常。 If neither of those sound familiar, I would look them up. 如果这些听起来都不熟悉,我会去找他们。

an example of try-catch 尝试捕捉的例子

try
{
//your code that may throw exception
}catch(TheException e){
e.printStackTrace(); // or however you handle exceptions
}

good resource for throwing exceptions: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html 引发异常的好资源: https : //docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html

Throwing an exception from one thread generally will not impact the runtime of the others. 通常,从一个线程引发异常不会影响其他线程的运行时。 Threads are kind of off in their own little world in that regard. 在这方面,线程在他们自己的小世界中有点过时。

That said, if two threads are sharing some state (whether directly through the heap or indirectly through a database or something like that) and one thread leaves the application's data in an unstable/corrupted state because it bombed out, that could obviously impact the behavior of the other threads, making them do further "bad stuff." 就是说,如果两个线程共享某个状态(无论是直接通过堆还是间接通过数据库或类似的状态),并且一个线程由于炸开了应用程序而使其数据处于不稳定/损坏状态,则可能明显影响行为其他线程,使它们做进一步的“坏事”。 You will want to prevent this whenever possible; 您将尽可能避免这种情况; best way to do it is to document your exceptions as best you can and to catch and handle them when thrown, and to back your logic with robust unit tests. 最好的方法是尽可能地记录您的异常,并在引发异常时对其进行捕获和处理,并使用健壮的单元测试来支持逻辑。

You can create a boolean variable maybe called running so that while your while loop is working fine, this variable will be true. 您可以创建一个布尔变量(可能称为running以便在while循环正常运行时,此变量为true。 Immediately an exception is caught, the variable becomes false. 立即捕获异常,变量变为false。 Your main task can then check the variable anytime it wants to use the shared data. 然后,您的主要任务可以在要使用共享数据时随时检查变量。 If the variable is true, read the data; 如果变量为true,则读取数据;否则为false。 else, do something else like restarting the thread depending on the type of exception caught. 否则,根据捕获的异常类型执行其他操作,例如重新启动线程。

Remember, in order to make the variable false, you will need to include its update in your catch clause. 请记住,为了使变量为false,您需要在catch子句中包含其更新。

There are better and more complex methods out there like the use of ExecutorService . 有更好更好的方法,例如使用ExecutorService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常是停止程序 - 如何防止它 - Exception is stopping the program - how to prevent it 如何修复我的 java 程序不停止运行? - How do I fix my java program not stopping running? 在kryonet中,如何在不停止程序的情况下与服务器断开连接? - In kryonet, how do I disconnect from a server without stopping my program? 如何防止程序陷入僵局,但仍保持互斥? (信号量) - how do i prevent my program from getting deadlock but still keep mutual exclusion? (semaphores) 当我停止我的程序时,如何防止我的 java 应用程序丢弃未确认的 RabbitMQ 消息? - How do I prevent my java application from discarding unacknowledged RabbitMQ messages when I stop my program? 当我的应用程序打开新的Java程序时,如何防止我的应用程序使用新主题重新粉刷自身? - When my app opens a new java program, how do I prevent my app from repainting itself with a new theme? 如何防止在程序启动时停止setCurrentItem()? - How to prevent stopping setCurrentItem() on program start? 如何在停止程序之前等待命令完成执行? - How do I wait for a command to finish executing before stopping the program? 如何在NamedQueries中防止运​​行时异常 - How do I prevent Runtime Exception in NamedQueries 如何防止Java中的无头异常? - How do I prevent a headless exception in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM