aspose file tools*
The moose likes Beginning Java and the fly likes using try without a catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using try without a catch" Watch "using try without a catch" New topic
Author

using try without a catch

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hello folks , can anyone explain how we can use a try with a finally and still the code compiles and gives the output.
As far as i understand we can never give a try without a catch , coz that is the entire point of exception handling , if we are not handling the exception the compiler should give an error.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
you can freely use a try without a catch. (in this case you need a finally block)
In the finally block you put operations that has to be performed in any case.

One example: try to invoke the following class with and without command line arguments:



Bye,
Nicola
Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Hi
In Java we can write try with out catch also, but at that time we must use finally.
If you are not using catch block with try, at that time if any exceptions are raised progaram, JVM will stop excuting remaining statements, but it executes finally block.




Gireesh Giri
Greenhorn

Joined: Apr 23, 2010
Posts: 23
Refer the following code.
First run the below program as it is and change the value 0 to some positive integer.

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

yeah i tried to run the run the code it runs displaying the messages in the try block throws the exception , how do i then expect my application to behave properly ie how can my application then work when some exception is thrown , wont the application terminate or give some error.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
how can my application then work when some exception is thrown , wont the application terminate or give some error.

That depends on what the application is doing. In the example code, the value of "x" isn't used for anything, so it doesn't matter than an exception was thrown during calculating it. Generally, a catch block either needs to put things into a state where the application can keep running, or it needs to re-throw the exception (or return an error code) for the calling method to handle.


Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The examples we've seen so far aren't very realistic; there's no reason why you'd ever write code like the above. But imagine a routine like this:



This routine opens a file, converts each line to an int, and returns the sum of the ints. It can fail in a number of ways: there can be an IOException while reading or closing the file, and there can be NumberFormatExceptions if the data is malformed. Whatever code calls this method definitely should catch those exceptions. But this routine just throws them to report the problem. But even if the data is malformed, the file has to be closed -- not closing files you read is always a bad thing. So we use try/finally so that we always close the file, even if the parseInt() or readLine() throws an exception.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using try without a catch