位置:首頁(yè) > 軟件操作教程 > 編程開(kāi)發(fā) > Java > 問(wèn)題詳情

Java 常見(jiàn)異常NoSuchMethod Exception

提問(wèn)人:劉旭39發(fā)布時(shí)間:2020-11-30

NoSuchMethod Exception

java.lang.NoSuchMethod Exception是方法不存在異常,通常是程序試圖通過(guò)反射來(lái)創(chuàng)建對(duì)象時(shí)引 發(fā)的異常。當(dāng)訪問(wèn)或修改某一個(gè)方法時(shí),系統(tǒng)無(wú)法找到該方法(一般是方法名定義錯(cuò)誤)則會(huì)拋 出 NoSuchMethod Exception 異常。

import java.lang.reflect.Method;

//NoSuchMethod Exception異常

public class Demo {

public static void main(String[] args){

try {

Class<Cat> cls = Cat.class;

Cat obj = cls.newInstance();

Method target = cls.getDeclaredMethod("desc", String.class);

}catch (InstantiationException e) {

System.out.println("捕獲異常: " + e.getClass().getName()); 

System.out.println("異常內(nèi)容為:" + e.getMessage());

}catch(NoSuchMethodException e) {

System.out.println("捕獲異常:" + e.getClass().getName()); 

System.out.println("異常內(nèi)容為:" + e.getMessage());

}catch(IllegalAccessException e) {

System.out.println("捕獲異常:"+ e.getClass().getName()); 

System.out.println("異常內(nèi)容為:" + e.getMessage());

}

}

}

D`U8R}B))GOG5M5G967M8AB.png

繼續(xù)查找其他問(wèn)題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部