Catching all Errors and Exceptions - Java Language Basics

Java examples for Language Basics:try catch finally

Description

Catching all Errors and Exceptions

Demo Code

import java.util.logging.Level;
import java.util.logging.Logger;

class BgThread extends Thread {
  Logger logger = Logger.getLogger("com.mycompany.mypackage");

  BgThread() {//from w w w  .  j a v a2 s  . c o m
    setDaemon(true);
  }

  // Set to true to shut down this thread
  boolean stop = false;

  public void run() {
    while (!stop) {
      try {
        // Perform work here
      } catch (Throwable t) {
        // Log the exception and continue
        logger.log(Level.SEVERE, "Unexception exception", t);
      }
    }
  }
}

Related Tutorials