关于Exception的Capture

Categories: Java; Tagged with: ; @ September 4th, 2013 0:14

今天写了段很挫的代码, 简单的来说就是这样:

	public static void main(String[] args) {
		try {
			throwNewException();
		} catch (Exception e) {
			System.out.println(e);
			System.out.println(e.getMessage());
		}
	}
	
	private static void throwNewException() {
		Object test = null; // simulate the Spring injection Error.
		test.getClass(); 
		throw new RuntimeException("UserName cannot be empty.");
	}

简直就是奇葩, 在做UnitTest时, 其中一个用例是throwNewException throw RuntimeException, 

由于Spring注入失败, 导致抛出NullPointerException, 而后续代码依赖于RuntimeException的ErrorMesg, 一直拿不到ErrorMesg. 纠结半天, 原因如下:

 

1.  懒. 使用RuntimeException而不是创建对应的Exception类型;

2.  懒. 在Catch时, catch了Exception, 而且没有打印Exception, 也就是说, 拦截并隐藏了所有Exception!

3.  懒.  Debug时懒得思考, 看到如下的输出:

java.lang.NullPointerException
null

就得出结论, Exception是Null的! WTF!! 然后就开始纠结是不是Spring的问题…

 

所以,

创建Exception类型, Capture具体Exception具体处理, 处理或是继续抛出Exception, 而不是隐藏,.

Debug remote JBoss application in Eclipse 在Eclipse中远程调试JBoss应用

Categories: Java; Tagged with: ; @ February 17th, 2012 22:50

#1. Config JBoss configuration file:

jboss-5.1.0.GA\bin: modify the configuration run.conf.bat(Windows) or run.conf(Linux), remove the comment at the start of the line, and set suspend=n:

(remove: rem or # )set “JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n” 

Start the server;

#2. Debug the project in Eclipse:

image



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.