Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RuntimeException(PL Help)

 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
1. Are all RuntimeExceptions thrown only at runtime? Do all Runtime Exceptions have a default handler because of which a compile time error never occurs ? . I mean the result is only a RunTimeException.
Are there any cases when the defualt handler doesn't work in the case of RunTimeException and an compile error is thrown?
2.
Do all checked exceptions throw a compile time error (and not runtime) if not caught by using try, catch or using throws? please explain.
3. Even in many checked exception even though I specified throws I had to still put a try catch in the caller of the method . If i don't give a try catch in the caller of the method , a compile time error still occurs.
I just wanted to confirm this point.
4. Is it true that try cannot be used on a single statement?
Thanks in advance
Padmini
 
anand raman
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. the runtime exception by default are handled by the run time engine and thus the compiler never bothers u to specify a try catch block.
2. the method has to completly handle all compile time exceptions or specify in the throws clause. This help the programmer using the interface to be aware of the things which a method can return.
3. i am not sure.. Any example..
4. why not ..
We all use
try {
BufferedReader br = new BufferedReader(new FileInputStream("file.txt"));
} catch (IOException e) {
System.err.println(e.getMessage());
}
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

3. Even in many checked exception even though I specified throws I had to still put a try catch in the caller of the
method . If i don't give a try catch in the caller of the method , a compile time error still occurs.
I just wanted to confirm this point.

Yes, if a method throws an Exception, then the statement which calls the method must be enclosed in a try-catch (or the method enclosing the statement which calls the original method must itself declare that it throws the exception). All checked Exceptions must be caught somewhere within a program or else thrown all the way up the calling chain to the JRE.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Appleton:
Yes, if a method throws an Exception, then the statement which calls the method must be enclosed in a try-catch (or the method enclosing the statement which calls the original method must itself declare that it throws the exception). All checked Exceptions must be caught somewhere within a program or else thrown all the way up the calling chain to the JRE.


********
Thanks scott and Anand.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic