简体   繁体   English

如何捕获在不同线程内引发的RuntimeException?

[英]How can I catch a RuntimeException that is thrown inside a different thread?

Some pseudocode of what i'm doing, 我正在做的一些伪代码,

public class MeasurementDevice implements SensorEventListener {  

        public MeasurementDevice() {
                    mSensorManager = (SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE);
                    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

                    HandlerThread mHandlerThread = new HandlerThread("sensorThread");
                    mHandlerThread.start();
                    Handler handler = new Handler(mHandlerThread.getLooper());
                    mSensorManager.registerListener(this, mAccelerometer, samplingPeriodHint, handler);
                    mSensorManager.registerListener(this, mGyroscope, samplingPeriodHint, handler);
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
           throw new RuntimeException();
        }  
}

But when the exception is thrown, i'm not really sure how I can catch it? 但是当抛出异常时,我不确定我怎么能抓到它? Any ideas? 有任何想法吗?

Literally speaking, you absolutely can't. 从字面上讲,你绝对不能。 Exceptions are thread-specific constructs. 例外是特定于线程的构造。 You would need any other form of inter-thread communication in Java. 您将需要Java中的任何其他形式的线程间通信。

This, of course, is the only thing that makes sense: exception throwing is a control flow mechanism that pops out of the stack. 当然,这是唯一有意义的事情:异常抛出是一种从堆栈中弹出的控制流机制。 If an exception happens in one thread, what do you want another thread to do? 如果在一个线程中发生异常,那么您希望另一个线程做什么? Terminate suddenly then jump to some unrelated exception handling code? 突然终止然后跳转到一些不相关的异常处理代码?

Of course you may want another chunk of code (ie class) to handle the exception, but this isn't a question of what thread the code is running on. 当然,您可能需要另一块代码(即类)来处理异常,但这不是代码运行的线程的问题。 That is of course possible, and IIRC it involves letting the exception propagate and using Thread.uncaughtExceptionHandler as a global router. 这当然是可能的,而IIRC涉及让异常传播并使用Thread.uncaughtExceptionHandler作为全局路由器。

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

相关问题 如何捕获在片段中引发的RuntimeException - How to catch RuntimeException thrown within a Fragment 在Java中,如何捕获从子线程引发的异常,然后从父线程再次引发异常 - In java how can I catch an exception thrown from child thread and then throw it again from the parent thread 如何在Guava重试器中捕获代码引发的异常? - How do I catch the exception thrown by the code inside a Guava retryer? 在lambda中抛出Catch RuntimeException并将其重新抛出为checked - Catch RuntimeException thrown in a lambda and rethrow it as checked 如果抛出RuntimeException,它是否可以作为异常捕获? - If RuntimeException is thrown, can it be caught as an Exception? 如何测试 ExecutorService 抛出的 RuntimeException - How to test an ExecutorService thrown RuntimeException 如何在Java中捕获AWT线程异常? - How can I catch AWT thread exceptions in Java? 如何捕获事件调度线程(EDT)异常? - How can i catch Event Dispatch Thread (EDT) exceptions? 如何使从线程引发的未经检查的异常传播到主线程? - How can I make an unchecked exception thrown from a thread to be propagated to main thread? 如何捕获JTA-Transaction内部抛出的异常? - How to catch a exception thrown inside of a JTA-Transaction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM