关于
java中异常处理的问题class JExceptionNew extends Exception
{
private static int m_number = 0;
public JExceptionNew()
{
m_number ++;
}
public String toString()
{
return("新异常出现"+ m_number +"次");
}
}
public class JExceptionnew
{
public static void main(String args[])
{
try
{
throw new JExceptionNew();
}
catch(JExceptionNew e)
{
System.err.println(e);
}
finally
{
System.out.println("finally语句块");
}
}
}
代码是用java中
自定义异常处理的程序,使用的NetBean7.1.1版本的IDE,运行后出现异常如下:
java.lang.NoClassDefFoundError: javastudy/JExceptionNew (wrong name: javastudy/JExceptionnew)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Exception in thread "main" Java Result: 1
成功生成(总时间:1 秒)
很奇怪,为什么提示成功生成,却得不到我想要的结果:新异常出现1次。求解释,是代码的问题还是IDE的问题???
------解决方案--------------------报的错误很明显了,没有找到对应的class;此错误为包路径问题,排查下,可以很快搞定的..
------解决方案--------------------JExceptionTest .java
Java code
class JExceptionNew extends Exception {
private static int m_number = 0;
public JExceptionNew() {
m_number++;
}
public String toString() {
return ("新异常出现" + m_number + "次");
}
}
public class JExceptionTest {
public static void main(String args[]) {
try {
throw new JExceptionNew();
} catch (JExceptionNew e) {
System.err.println(e);
} finally {
System.out.println("finally语句块");
}
}
}
新异常出现1次
finally语句块
------解决方案--------------------
JAVA 是区分大小写的!