未处理的异常:java.lang.ClassNotFoundException错误 [英] unhandled exception: java.lang.ClassNotFoundException Error

查看:865
本文介绍了未处理的异常:java.lang.ClassNotFoundException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类代码,并且在进行调试时,它始终显示以下错误:

I have the following Class code and when Debug it kept showing the following error:

1。)未处理的异常:java.lang.ClassNotfoundException
2 。)未处理的异常:java.lang.NoSuchMethodException

1.) Unhandled Exception: java.lang.ClassNotfoundException 2.) Unhandled 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:

You have to add all your exceptions or use a 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也被删除,因为它是(根据调试器)永不抛出。

Also the IOException you were catching, is removed since it is (according to the debugger) never thrown.

这篇关于未处理的异常:java.lang.ClassNotFoundException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆