Using a ProxySelector

Here’s a working example that says everything. MyProxySelector is automatically contacted when URLConnection is about to make a connection.

Main.java:

import java.net.*;
import java.io.*;
import java.util.*;

public class Main
{
   public static void main(String []args) throws Exception {
      if (args.length != 1) {
         System.err.println("Usage: java Main <URL>");
         System.exit(0);
      }
      
      System.out.println("Old ProxySelector: " + ProxySelector.getDefault());
      ProxySelector.setDefault(new MyProxySelector());
      System.out.println("New ProxySelector: " + ProxySelector.getDefault());
          
      URL url = new URL(args[0]);
      URLConnection connection = url.openConnection();
 
      connection.setConnectTimeout(5000);
      connection.connect();
   }
}

class MyProxySelector extends ProxySelector
{
   public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
      System.out.println("connectFailed: " + ioe.getMessage());
   }
   
   public List<Proxy> select(URI uri) {
      ArrayList<Proxy> list = new ArrayList<Proxy>();
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.esus.com", 8080));
      list.add(proxy);

      return list;
   }
}

Catching exceptions in threads in JDK1.5

Sometimes, a thread may abruptly terminate, for example due to a NullPointerException. In large applications, a bug due to a thread that vanished just like that may be very hard to spot. In JDK1.5, there is a new inner interface Thread.UncaughtExceptionHandler that will make it a lot easier to detect exceptions.

When a thread is about to terminate because of an uncaught exception, the thread’s uncaughtExceptionHandler is called. If the thread’s uncaughtExceptionHandler is null, the thread’s ThreadGroup (which implements UncaughtExceptionHandler) is called. The ThreadGroup‘s uncaughtExceptionHandler calls the Thread DefaultUncaughtExceptionHandler if it is set otherwise it prints out the exception to System.err.

Excerpt from ThreadGroup.java:

    public void uncaughtException(Thread t, Throwable e) {
	if (parent != null) {
	    parent.uncaughtException(t, e);
	} else {
            Thread.UncaughtExceptionHandler ueh = 
                Thread.getDefaultUncaughtExceptionHandler();
            if (ueh != null) {
                ueh.uncaughtException(t, e);
            } else if (!(e instanceof ThreadDeath)) {
		System.err.print("Exception in thread ""
				 + t.getName() + "" ");
                e.printStackTrace(System.err);
            }
        }
    }

The following example forces a NullPointerException in a Thread which is dealt with with our own MyUncaughtExceptionHandler.

Main.java:

public class Main
{
   public static void main(String []args) {
      Thread t = new Task();
      t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
      t.start();
   }
}
 
class Task extends Thread
{
   private Object o = null;
 
   public void run() {
      o.toString();
   }
}
 
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler
{
   public void uncaughtException(Thread t, Throwable e) {
      System.out.println("Exception occurred in Thread " + t);
 
      e.printStackTrace();
   }
}