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

Exceptional Handling

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • 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 ]
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can you paste your code here after adding try/catch block ?

Thanks
Amit
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • 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
    • Quote
    • Report post to moderator
    Thanks for all your replies.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50196
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Arjun Reddy:
    Thanks for all your replies.
    You're welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic