[英]How to get Exception Class from Exception Name
I have a string variable that has a full qualified exception name.我有一个具有完整限定异常名称的字符串变量。 I want to check in catch block if exceptions occur whether it is an instance of exception that mentioned in string or not.如果发生异常,我想检查 catch 块,无论它是否是字符串中提到的异常实例。 How to solve that如何解决
String stringEx = "org.hibernate.StaleStateException";
try {
// program
} catch (Exception ex) {
if (e instanceof stringEx) { //<-- How to convert string to exception class
// do specific process
}
}
Maybe you need this:也许你需要这个:
String stringEx = "org.hibernate.StaleStateException";
try {
// program
} catch (Exception ex) {
if (Class.forName(stringEx).isInstance(ex)) {
// do specific process
}
}
I have a string variable that has a full qualified exception name.我有一个具有完全限定异常名称的字符串变量。 I want to check in catch block if exceptions occur whether it is an instance of exception that mentioned in string or not.如果发生异常,我想检查 catch 块是否是字符串中提到的异常实例。 How to solve that如何解决
String stringEx = "org.hibernate.StaleStateException";
try {
// program
} catch (Exception ex) {
if (e instanceof stringEx) { //<-- How to convert string to exception class
// do specific process
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.