0

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. 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
        }
    }
1

1 Answer 1

6

Maybe you need this:

String stringEx = "org.hibernate.StaleStateException";
try {
    // program
} catch (Exception ex) {
    if (Class.forName(stringEx).isInstance(ex)) { 
        // do specific process
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.