说是字段不存在的错误
Topic source后来看了其他人的作业,发现两个问题
1. set()里面我错误地用了Class实例c而不是Person实例p
2.Main函数中没有throws Exception
关于第二点有些疑惑,为什么会出现Exception需要throws呢,难道不是应该没有异常的吗
异常需要捕获,如果不捕获就要向上抛出。
你调用的方法它在字段不存在的时候抛出异常,抛到你执行的这段代码,你必须在这里处理异常。
你认为的没有异常只是你认为你写的代码是不会发生异常的,你可以写一个不存在的字段试试,异常就会被捕获或抛出了。
- 1
零度的花火
运行后报错:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type NoSuchFieldException
Unhandled exception type NoSuchFieldException
Unhandled exception type IllegalAccessException
Unhandled exception type IllegalAccessException
包也导入了,在eclipse中相应的行显示Unhandled exception type NoSuchFieldException,问题是这个字段是有的啊,并且用的是 c.getDeclaredField("name");并不存在private的问题,为什么呢