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

Java 常見異常NoSuchMethod Exception

提問人:劉旭39發(fā)布時間:2020-11-30

NoSuchMethod Exception

java.lang.NoSuchMethod Exception是方法不存在異常,通常是程序試圖通過反射來創(chuàng)建對象時引 發(fā)的異常。當訪問或修改某一個方法時,系統(tǒng)無法找到該方法(一般是方法名定義錯誤)則會拋 出 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("異常內容為:" + e.getMessage());

}catch(NoSuchMethodException e) {

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

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

}catch(IllegalAccessException e) {

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

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

}

}

}

D`U8R}B))GOG5M5G967M8AB.png

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

相關視頻回答
回復(0)
返回頂部