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 Exceptional Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptional Handling" Watch "Exceptional Handling" New topic
Author

Exceptional Handling

Arjun Reddy
Ranch Hand

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

Be Humble... Be Nice.
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
Hi,
can you paste your code here after adding try/catch block ?

Thanks
Amit
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

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

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
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

    Joined: Nov 10, 2007
    Posts: 627
    Thanks for all your replies.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39386
        
      28
    Originally posted by Arjun Reddy:
    Thanks for all your replies.
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exceptional Handling