• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

catch block.....

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net



Output is:
Try block1
Catch1:java.io.IOException: exception1
Finally block1:
But here method is throwing an exception which is caught by catch block in main method.
Why it is not printing �catch2:� in output?

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception is caused during int u=method();

When assigning the value it calls the method. And in the method Exceptionm is caused .which leads to its cath and finally{} and execution ends...
it won't even execute the return statement.
[ December 03, 2008: Message edited by: James Tharakan ]
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception thrown is handled only once, not multiple times.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because you have already caught your exception in the catch block of method().. Exceptions once caught are not caught again.

If you did not have a catch() block in method() AND if you had only declared that exception in method(), then the "catch2" would have been executed.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why it is not printing �catch2:� in output?



Because you have already catched the exception in catch block of the method() . So,rest of the code in the main will continue after
.

Normally you wouldn't do this in real development. You are announcing that the method throws the IOException but you have catched it inside the method itself.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,
It is caught and handled in method() so why it will be caught in main() again.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i am not catching exception in main mathod it is giving me compilation error as

IOException must catch or throw...

so i should catch..

do you mean that it is just to specify not to catch because it is already caught in method?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the signature of your method

static int method() throws IOException

which says that the method which will call it has to handle the exception if thrown.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the method your are throwing an exception and you are also catching the Exception.
So the exception is handled.
But in the signature of the method ,
static int method() throws IOException
your are saying that the method would throw an exception which it will not handle. In such a case the main method shouuld handle it.

you can very well do as follows




 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes main method should handle that IOException so i am catching it.

but already IOException is handled in method so it is not handled in main method because exception should handled only onetime...thats why it is not displaying "catch2" in output...

am i right?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic