+ 3

Why using Exception e Only

we can also use different exception statement right ?

5th Jun 2017, 4:50 AM
sudhir chakravarthi
sudhir chakravarthi - avatar
2 Answers
+ 10
There's all kinds of exception you can catch, and you can do something different for each kind of exception, all in one try. But if the specific exception doesn't matter, then Exception works fine as a generic catch-all. Ex: try { /*Scanner input for an int*/ } catch (InputMismatchException e) { /*handling for if number cannot be assigned to int*/ } catch (NoSuchElementException e) { /*handling for if no input is provided/available*/ } catch (IllegalStateException e) { /*handling for if Scanner has been closed*/ }
5th Jun 2017, 6:18 AM
Tamra
Tamra - avatar
+ 2
Yes, you can use any variable for e. It's pretty typical to see an all lowercase acronym for the exception though (Exception => e, InvalidStateException => ise, SomeReallyLongException => srle, etc). Also you can catch any other exceptions you want to (most likely you want a more specific one than Exception).
5th Jun 2017, 6:18 AM
Jason Runkle
Jason Runkle - avatar