Articles

How to throw checked exception message inside Runnable run method?

advantage-of-java-certification
Written by Mushfiq Mammadov
package az.mm.runnable;

import java.io.IOException;

public class Controller {

    public static void main(String[] args) {     
        Service service = new Service();
        service.sendMessage();   
    }
}

class Service {  

    void sendMessage() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    throw new IOException();
                } catch (IOException ex) {
                    // throw ex; // compile error
                    throw new RuntimeException(ex);
                }
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }
}

About the author

Mushfiq Mammadov

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.