读书人

利用二路分发实现JAVA错误处理

发布时间: 2012-09-22 21:54:54 作者: rapoo

利用二路分发实现JAVA异常处理
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

public class ExceptionHandler
{
public interface ExceptionItem
{
Class<?> exceptionType();

void display();
}

public void handleException(Exception e)
{
ExceptionItem exceptItem = AbstractExceptionItem.wrapException(e);
displayExceptionInfo(exceptItem);
}

private final void displayExceptionInfo(ExceptionItem item)
{
item.display();
}

private abstract static class AbstractExceptionItem implements
ExceptionItem
{
private static Map<Class<? extends Exception>, Class<? extends ExceptionItem>> innerMap = new HashMap<Class<? extends Exception>, Class<? extends ExceptionItem>>();
static
{
innerMap.put(FileNotFoundException.class,
FileNotFoundExceptionItem.class);
innerMap.put(SocketTimeoutException.class,
SocketTimeoutExceptionItem.class);
}

protected Class<?> exceptionType;

protected Exception e;

protected AbstractExceptionItem(Exception e)
{
this.e = e;
this.exceptionType = e.getClass();
}

private static final ExceptionItem wrapException(final Exception except)
{
final Class<?> exceptionType = except.getClass();
Class<? extends ExceptionItem> cls = innerMap.get(exceptionType);
if (null == cls)
return null;
try
{
Constructor<? extends ExceptionItem> constructor = cls.getDeclaredConstructor(new Class<?>[] { Exception.class });
return constructor.newInstance(except);
}
catch (SecurityException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (NoSuchMethodException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
}

private static final class FileNotFoundExceptionItem extends
AbstractExceptionItem
{
FileNotFoundExceptionItem(Exception e)
{
super(e);
}

@Override
public Class<?> exceptionType()
{
return exceptionType;
}

@Override
public void display()
{
System.out.printf("The file [%s] is not found!", e.getMessage());

}
}

private static final class SocketTimeoutExceptionItem extends
AbstractExceptionItem
{
SocketTimeoutExceptionItem(Exception e)
{
super(e);
}

@Override
public Class<?> exceptionType()
{
return exceptionType;
}

@Override
public void display()
{
System.out.printf("The socket connect timed out, detail message is:[%s]",
e.getMessage());
}
}

/**
* @param args
*/
public static void main(String[] args)
{
ExceptionHandler instance = new ExceptionHandler();
instance.handleException(new SocketTimeoutException("connection refused to 10.60.7.69"));
}
}

读书人网 >编程

热点推荐