File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Runtime Exceptions, Do they need try-catch at all?/ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime Exceptions, Do they need try-catch at all?/" Watch "Runtime Exceptions, Do they need try-catch at all?/" New topic
Author

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

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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 ]

Be Humble... Be Nice.
Mark Freeman
Ranch Hand

Joined: Aug 02, 2006
Posts: 40
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

Joined: Aug 16, 2005
Posts: 14278
    
  21

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks Mark and Jesper.
Paul Sisco
Greenhorn

Joined: Jun 15, 2008
Posts: 11
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.
 
 
subject: Runtime Exceptions, Do they need try-catch at all?/