未处理的异常:java.lang.ClassNotFoundException 错误
unhandled exception: java.lang.ClassNotFoundException Error
我有以下 Class 代码,在调试时它一直显示以下错误:
1.) 未处理的异常:java.lang.ClassNotfoundException
2.) 未处理 Exception:java.lang.NoSuchMethodException
错误日志:
/apps/robotapp/Utils.java:32: error: exception IOException is never thrown in body of corresponding try statement
} catch (IOException e) {
^
/apps/robotapp/Utils.java:24: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:24: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:24: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:26: error: unreported exception NoSuchMethodException; must be caught or declared to be thrown
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
^
/apps/robotapp/Utils.java:26: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
^
Note:
/RobotService.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
9 errors
FAILED
有错误的代码段:
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (final NumberFormatException e) {
throw new IOException(e);
} catch (final IndexOutOfBoundsException e) {
throw new IOException(e);
} catch (IOException e) {
throw new IOException(e);
}
您必须添加所有例外情况或使用 catch all:
try {
} catch(Exception exp) { }
更正后的代码:
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (final NumberFormatException e) {
throw new IOException(e);
} catch (final IndexOutOfBoundsException e) {
throw new IOException(e);
} catch (java.lang.ClassNotfoundException e) {
throw new IOException(e);
} catch (java.lang.NoSuchMethodException e) {
throw new IOException(e);
} catch (IllegalAccessException e) {
throw new IOException(e);
}
您捕获的 IOException 也被删除,因为它(根据调试器)从未抛出。
将代码更改为
try {
//do something
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("Error occured");
}
或
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (ClassNotFoundException|NoSuchFieldException|IllegalAccessException|NoSuchMethodException e) {
throw e;
}
干净多了,对吧?
我有以下 Class 代码,在调试时它一直显示以下错误:
1.) 未处理的异常:java.lang.ClassNotfoundException 2.) 未处理 Exception:java.lang.NoSuchMethodException
错误日志:
/apps/robotapp/Utils.java:32: error: exception IOException is never thrown in body of corresponding try statement
} catch (IOException e) {
^
/apps/robotapp/Utils.java:24: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:24: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:24: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:25: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
^
/apps/robotapp/Utils.java:26: error: unreported exception NoSuchMethodException; must be caught or declared to be thrown
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
^
/apps/robotapp/Utils.java:26: error: unreported exception IllegalAccessException; must be caught or declared to be thrown
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
^
Note:
/RobotService.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
9 errors
FAILED
有错误的代码段:
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (final NumberFormatException e) {
throw new IOException(e);
} catch (final IndexOutOfBoundsException e) {
throw new IOException(e);
} catch (IOException e) {
throw new IOException(e);
}
您必须添加所有例外情况或使用 catch all:
try {
} catch(Exception exp) { }
更正后的代码:
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (final NumberFormatException e) {
throw new IOException(e);
} catch (final IndexOutOfBoundsException e) {
throw new IOException(e);
} catch (java.lang.ClassNotfoundException e) {
throw new IOException(e);
} catch (java.lang.NoSuchMethodException e) {
throw new IOException(e);
} catch (IllegalAccessException e) {
throw new IOException(e);
}
您捕获的 IOException 也被删除,因为它(根据调试器)从未抛出。
将代码更改为
try {
//do something
} catch (Exception e) {
e.printStackTrace();
//throw new Exception("Error occured");
}
或
try {
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
final int tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long)os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
return t * msInSec / tck;
} catch (ClassNotFoundException|NoSuchFieldException|IllegalAccessException|NoSuchMethodException e) {
throw e;
}
干净多了,对吧?