File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The try catch block.

 
Varun Goenka
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does the try catch block do.
Even if there are no issues with the input strings , should they not be validated within the JVM rather than within the try block.
Also does my analogy with the If Else blocks hold.

if(condition) try
{//do something} {condition}
Else{ catch{
//Do something} //Do something}


Please help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your analogy with if-else doesn't hold.

A "try" encloses code which might suffer an error (expressed as an Exception) and a "catch" encloses code which ought to be executed if and only if that error occurs (ie, that Exception is thrown). If an Exception occurs part-way through a "try" then only part of the "try" is actually executed.

A "finally" encloses code which will be executed after a "try" and any associated "catch"es have completed.

And simple validation is probably better done without Exceptions. You might do well to throw an Exception if the validation fails, however.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if there are no issues with the input strings , should they not be validated within the JVM


what type of validation or issues your are talking about
 
Krishnamurthy Subramanian K
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

I understood what exactly you want.

If you know what exaclty needs to be checked then as you said you can use "if----else---" block.
If you don't know the exact condition like, "if you are trying to access a file, you are not sure whether the files is available in a location specified or not". In such a situation if you enclose the file access logic inside the try block it will throw an exception by entering the catch block, if the file object does not exists. At the same time if it is availbale it will run smoothly.


regards
Krishna
 
Varun Goenka
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was that if an error of any sort occurs during runtime while alues are being put in, should the error not be handled outside the TRY block.
Well I really sounded incoherent because i never really had a good idea of I was talking about in the first plae.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a Reader for inputting values, you will have to handle Exceptions because most of their methods declare IOException or FileNotFoundException, which are checked Exceptions.
If you use the parseXXX methods (eg Double#parseDouble()) it is worthwhile using a try-catch for the NumberFormatException they throw (an unchecked Exception), and you can do something similar for Scanne without using Exceptionsr; look at this thread.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like the posts before mine have explained a Try-Catch block is used to handle exceptions that would otherwise cause your program to fail.

Example: If you were trying to get a number for the user and the user enters an 'a'. This would cause an exception to be thrown by the JVM, and your program would fail.

It's also a good practice with Try-Catch blocks to handle specific exceptions, however if you don't know the name of the exception or if there will be an exception you can use a generic exception that will catch all exceptions.

Generic Hander:


Specific Handler:



Hope this helps.

Hunter.
 
supriya riya
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading the entire discussions and i think try catch help to avoid abnormal terminations of program.When we use try catch we know exact reason of exception or program faliure.Else Program abruptly terminates
As Hunter McMillen said
a Try-Catch block is used to handle exceptions that would otherwise cause your program to fail.


 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic