• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using try without a catch

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic