This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
Exception
__+--RuntimeException
_______+-- IndexOutOfBoundsException
________________+-------ArrayIndexOutOfBoundsException
________________+-------StringIndexOutOfBoundsException
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?

Thanks!
[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?
Thanks!
[ August 18, 2003: Message edited by: Roger Zhao ]
 
Bartender
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).
Regards,
Jeff
[ August 25, 2003: Message edited by: Jeff Bosch ]
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!