Check if a variable exists in a class [closed]

0

I'm trying to check if a variable exists in a class. For this I am using the code:

try{
    clazz.getDeclaredField("id");
} catch(Exception e){
    System.out.println(e.getMessage());
}

With this I can see without problems.

My Problem: When the variable does not exist, java prints the name of the variable that does not exist in the console, I need this information not to appear if the variable does not exist. How do I not do this? Can anybody give me a light?

EDIT: even removing System.out.println (e.getMessage ()); it continues to print the name of the variable that does not exist, this is what I do not want to appear

    
asked by anonymous 16.10.2017 / 23:07

1 answer

0

Are you sure you do not want to do anything if the variable does not exist? This is likely to be a programming error and should be fixed. Exception is not for this.

If you want to insist just do not print anything:

try{
    clazz.getDeclaredField("id");
} catch(Exception e) {}
    
16.10.2017 / 23:42