Know who is throwing exception
Answered
Is there any shortcut to know who is throwing an exception just by the catch? Like the hierarchy shortcut (ctrl+alt+h)?
try {
// code block
} catch (RuntimeException e) { <---
}
Please sign in to leave a comment.
When you place cursor on the catch keyword, IDE will show you the places which produce the related exception. The same does Main Menu | Edit | Find Usages | Find Usages in File action (Ctrl/Cmd+F7 default shortcut):
Thank you, Andrey. But the problem is if the exception is thrown inside some inner class.
With this action IDE shows all the places inside the try block where the exception might be thrown. What do you mean by inner class? Please provide a sample. Thanks.
Like this
You throw the Runtime exception here. And do not have the throws clause in the method signature.
1. The method must have throws clause in its declaration when this method throws the checked exception.
2. Runtime exceptions are not checked exceptions that means that the compiler does not require that you handle the such an exception in a catch, or declare your method as throwing it.
But if the method contains the throws in method assignature in class ClassTwo, Intellij show the places which produce the related exception?
ClassOne.doAny() must contain the throws in the method signature if it throws the checked exception. Note that this works only for the checked exceptions.
Ok, only for checked exceptions. Thank you.