I really want to use features from Java-1.7. One of this feature is "Multi-Catch". Currently I have the following code
try {
int Id = Integer.parseInt(idstr);
TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id));
updateTotalCount(tempTypeInfo);
} catch (NumberFormatException numExcp) {
numExcp.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
I want to remove the two catch blocks from the above code, and instead use single catch like below:
try {
int Id = Integer.parseInt(idstr);
TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id));
updateTotalCount(tempTypeInfo);
} catch (Exception | NumberFormatException ex) { // --> compile time error
ex.printStackTrace();
}
But the above code is giving compile time error:
"NumberFormatException" is already caught by the alternative Exception.
I understood the above compile time error but what is the replace for my first block of code.
catch
blocks to catch different types of exceptions in order of specific to general instead of using aninstanceof
for each type of exception. – Haiphong