Skip to main content

Handling unexpected runtime exception inside Thread. thread.setUncaughtExceptionHandler

Create and set UncaughtExceptionHandler before starting new thread to catch unexpected runtime exception in Multithreading application.

package demo;

public class ExceptionHandler {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                throw new RuntimeException("Critical Error");
            }
        });
        System.out.println("Starting new thread " + Thread.currentThread().getName());
        //Throw this exception if any exception has occurred inside running thread but was not caught anywhere else
        //This should be set before starting the new thread
        thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                System.out.println("Error occurred on thread: " + thread.getName() + ". Reason : " + throwable.getMessage());
            }
        });

        thread.setName("Bad Thread");

        thread.start();

    }
}


Comments

Popular posts from this blog

Live video streaming with ffmpeg and ffserver

Live video and audio streaming with ffmpeg and ffserver This is a guide on how to stream video using ffserver and ffmpeg in ubnutu. The ffmpeg ffserver version used is 3.3-1~16.04.york1. You can download ffserver from ffmpeg.org . I'm using linux mint 18. So, I've downloaded ffmpeg from the repository. You can follow the link below to install the latest version of ffmpeg. Install ffmpeg in linux mint 18 using command line . First, we need to setup our ffserver.conf to stream the audio and video properly. ffserver.conf file is usually located in /etc/ffserver.conf. You can either edit the ffserver.conf or create your own .conf file. I'll be creating my own .conf file named livestream.conf In console, type sudo nano /etc/livestream.conf Blank .ffm file will be created. Now, enter the required properties for streaming live video using ffserver #Default port HTTPPort 8090 HTTPBindAddress 0.0.0.0 MaxHTTPConnections 2000 MaxClients 1000 MaxBandwidth 100000 Custo...

Spring Security with Spring Boot and MySQL

Spring Security with Spring Boot and MySQL In this tutorial I'll show how to use Spring Security with Spring Boot and MySQL. We'll implement a UserDetailsService provided by Spring Security. It is easy to configure Spring Security with Spring Boot and MySQL. You can also check how to run Spring Boot application inside docker container Steps: 1. Create a Spring Boot project from start.spring.io with following dependencies in your pom.xml file. You can choose gradle also for project dependencies. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- dev tools for hot reloading --> <dependency> <groupId>org.springframework.b...