Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Catch RuntimeException  RSS feed

Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all:
Here is the hierarchy of Exception related to array index errors.
_______+-- IndexOutOfBoundsException
Suppose you had a method X which could throw both array index and string index exceptions. Assuming that X does not hava any try-catch statements, which of the following statments are correct?

[a]. The declaration for X must include "throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException".
[b]. if a method calling X catches IndexOutOfBoundsException, both array and string index exceptions will be caught.
[c]. if the declaration for X includes "throws IndexOutBoundsException", any calling method must use a try-catch block.
[d].the declaration for X does not have to mention exceptions.
Answer: [b], [d]
The explaination about option 'c' & 'd' is
[c]-- the significant word here is "must". Because these exceptions descend from RuntimeException, they do not have to be caught exen if declared by method X
<Q1>: "they do not have to be caught", is it meaning that any calling method X can throw these exception to the method who calling them, so "they do not have to be caught" ?
[d]--Because these exception descend from RuntimeException, they do not have to be declared.
<Q2>:why? Something special for RuntimeException?
Another <Q3>: What about the important difference between RuntimeException and CheckedException when catching them?
[ August 18, 2003: Message edited by: Roger Zhao ]
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions 1 and 2:
Yes! Runtime Exceptions are special cases. You can throw them without declaring that they can be thrown. If you had to declare them, then any method that worked with Objects would have to declare that it could throw a NullPointerException (which, too, is a RuntimeException) So, expressing this another way, all methods have an implicit "throws RuntimeException" modifier, since any method can throw one.
Question 3:
There is not difference when catching the exception. You would catch it just as you would catch a CheckedException.
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the same holds true for Error as well, because it, like Exception and RuntimeException, descends from Throwable. Anything that's Throwable can be caught, though you probably wouldn't want to catch everything (especially errors).
[ August 25, 2003: Message edited by: Jeff Bosch ]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!