• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptional Handling

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I wrote the following code without the try catch blocks.



And it gave me the error:


Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionalHandling.add(ExceptionalHandling.java:10)
at ExceptionalHandling.main(ExceptionalHandling.java:20)


Now, when I used the try-catch blocks, it gave the following error as:

java.lang.ArithmeticException: / by zero
at ExceptionalHandling.add(ExceptionalHandling.java:10)
at ExceptionalHandling.main(ExceptionalHandling.java:20)


So, in both the cases its telling me that ArithmeticException has occured. Now my question is why use the try catch block then?

Thanks.
[ June 24, 2008: Message edited by: Arjun Reddy ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can you paste your code here after adding try/catch block ?

Thanks
Amit
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.ArithmeticException is a Runtime exception. It is thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.


Well, the use of try-catch blocks in now tampers the type of xception. Try-catch blocks help you to run your explicit code in case an exception is occured. The specific piece of code will run in catch block, if it is present, or else the program will terminate abruptly.

Thus, the type of exception will always be the type it is, be the code have try-catch or not.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it depends on what type of exception you catch in the try-catch block.

Not catching the appropriate and required exception is as same as having the code without a try-catch block. It does not save the purpose.

If you have a try-catch block and when the appropriate expected exception is thrown at runtime the programmer/developer has a facility to take a action which can be of a corrective measure or an intimation to the user with the customized message etc., Without which, the user has no means to stop the abrupt processing with the stack trace as what you have pasted!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arjun Reddy:


So, why use the try catch block then?

It just so happens that both methods of handling the Exception seem to use printStackTrace().

Without the try-catch, the Exception propagates until it reaches the JVM, and the JVM does two things (at least):
  • It prints the stack trace, and
  • It terminates the thread with the Exception in.
  • If you only have one thread, then the JVM terminates as well.

    With the try-catch, assuming you catch the right sort of Exception, the thread continues to run to completion.

    Lots more about it in the Java Tutorials.
     
    Arjun Reddy
    Ranch Hand
    Posts: 629
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all your replies.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Arjun Reddy:
    Thanks for all your replies.

    You're welcome
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic