How to make a java Thread unstoppable without catching Throwable

101 views Asked by At

I want to make a thread unstoppable for some reason, what is the sonarcube compliant solution? I have to catch Throwable but.

It says: Catch Exception instead of Throwable. Throwable and Error should not be caught

I cannot only catch Exceptions

1

There are 1 answers

0
Aisu On

Here's an example of how to catch and rethrow exceptions to comply with the rule:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        try {
            // Your thread logic here
        } catch (Exception e) {
            // Handle specific exceptions if needed
            // Rethrow as unchecked exception or wrap and rethrow
            throw new RuntimeException(e);
        }
    }
}