• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

try/catch/finally

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What all gets printed when the following gets compiled and run. Select all correct answers.

public class test {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j--;
if(i/j > 1)
i++;
}
catch(ArithemticException e)
{
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(1);
}
catch(Exception e)
{
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}


A.0
B.1
C.2
D.3
E.4
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Irfan,

The o/p will be 0,3,4.
Reason---
if(i/j>1) throws ArithmeticException that is caught by
catch(ArithmeticException ae) -----> prints 0
finally block gets executed -----> prints 3
control continues after finally ----> prints 4
The points that gets clear from ur code is
1-> finally block gets executed in every case i.e. Exception or no exception .
2-> after finally / catch block the program continues through the next block of statements.

regards
deekasha




 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deekasha is right.
But Irfan, what are you trying to prove here?
Good Luck
Ambrose
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deeksha,
I think you are a little bit wrong.
After finally gets executed, the flow goes back to the calling method which calls the present method( not the next block of statements).
I agree that your o/p is correct.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi danny,
if u agree with my answers then u should agree that after executing finally block the control moves to next block of statement (if present).otherwise how would 4 get printed.
in case there is nothing written in the function after the finally block then certainly the control will get transferred to the caller function.
now do u agree with me ........
deekasha
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deekasha is correct. The try/catch is used to catch an exception and process it so that program flow can continue.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error will it give if there is no catch for a try but there is a finally?

[This message has been edited by charu (edited October 17, 2000).]
[This message has been edited by charu (edited October 17, 2000).]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charu,
The finally block will get executed wether or not
the exception is thrown or handled. The only time the finally will not get executed is when you invoke the System.exit(); On all other times finally will be called.
Pichai
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
try block must be at least followed by either a catch block or a finally block.or both may be there.
so in case there is no catch after try block but still finally block is there then it's okay with the compiler.
in case there are both catch and finally blocks then finally block shall be the last.
regards
deekasha
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if it was clearly stated above. However, the last line of code was only executed because the exception was caught`.
Had the exception not been handled, the finally block would have been executed, execution of the mothod would have ceased, and the exception would have been thrown to the calling method (assuming this method could throw this exception).
 
Richard Mack
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In regards to previous...
Since this a main method, execution of the program would have halted.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic