• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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

 
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 ]
 
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
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.
 
You got style baby! More than this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!