Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime Exceptions, Do they need try-catch at all?/

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Do runtime exceptions need a try-catch blocks at all? Coz for ArithmeticException(Runtime Exception), even tho I did not put it in the try catch blocks, I was able to compile the code properly(It gave me an error at runtime.Even tho I put em in the try-catch, it still gives me an error at runtime only). But for SqlException(Compile time exception), if I don't have a try-catch blocks, It's giving me a compile time error.

Thanks.
[ June 26, 2008: Message edited by: Arjun Reddy ]
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjun. No it is not necessary (or even ideal) to catch a runtime exception. Here is an interesting post I found by using google and searching for "catch a runtime exception".

http://forum.java.sun.com/thread.jspa?threadID=669492&messageID=3914835

Mark
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has two types of exceptions: checked and unchecked exceptions.

Exception classes that inherit from RuntimeException are unchecked exceptions. Other exception classes are checked exceptions.

See The Catch or Specify Requirement in Sun's Java tutorial about exceptions. (It mentions three kinds of exceptions: checked, unchecked and errors).
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark and Jesper.
 
Paul Sisco
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arjun Reddy:
Hi,

Do runtime exceptions need a try-catch blocks at all? Coz for ArithmeticException(Runtime Exception), even tho I did not put it in the try catch blocks, I was able to compile the code properly(It gave me an error at runtime.Even tho I put em in the try-catch, it still gives me an error at runtime only). But for SqlException(Compile time exception), if I don't have a try-catch blocks, It's giving me a compile time error.

Thanks.

[ June 26, 2008: Message edited by: Arjun Reddy ]


The reason to have a try/catch is so you can handle the exception how ever you want, instead of letting another part of the system handle it for you. There are times when you want to handle the exception locally. In that case, a try/catch is what you need. There are other cases where you want your method to throw an exception. This way whatever called that method can handle the exception.

The method you are calling that requires the try/catch at compile time is explicitly defined to throw an exception. So you are required to either catch it, or define your method to throw it.

I hope this makes sense. Please let me know if it doesn't, and I will try again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic