aspose file tools*
The moose likes Beginning Java and the fly likes The try catch block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The try catch block." Watch "The try catch block." New topic
Author

The try catch block.

Varun Goenka
Ranch Hand

Joined: Mar 09, 2009
Posts: 37
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.


From The Demon,
with love.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
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

Joined: Apr 15, 2008
Posts: 131
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


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Krishnamurthy Subramanian K
Greenhorn

Joined: Jan 12, 2009
Posts: 5
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

Joined: Mar 09, 2009
Posts: 37
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

Joined: Oct 13, 2005
Posts: 36482
    
  16
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

Joined: Mar 13, 2009
Posts: 492

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.


"If the facts don't fit the theory, get new facts" --Albert Einstein
supriya riya
Ranch Hand

Joined: Feb 23, 2009
Posts: 41
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
 
subject: The try catch block.
 
Similar Threads
Please help me with this
throw clause in exceptions
Exam Objectives Flow Control.
Strange NullPointerException phenomenon
Purpose of Finally Block