This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can somebody please explain me the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can somebody please explain me the output" Watch "Can somebody please explain me the output" New topic
Author

Can somebody please explain me the output

Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
Hi

I have come across one more question in java blackbelt

I am not able to understand the output.



The output is BC

Regards
PAdma
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Padma Asrani:
Hi

I have come across one more question in java blackbelt

I am not able to understand the output.



The output is BC

Regards
PAdma


The method test() does not throw an exception to the main method.

The exception is thrown and caught within the try/catch block inside test() and prints out B. The finally block executes and prints out C.
Prashanth Tonupunuri
Greenhorn

Joined: Jun 25, 2007
Posts: 1
hi padma ji..what is java blackbelt...i donno can you tell me...
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Hi Keith,
Originally posted by Keith Lynn:


The method test() does not throw an exception to the main method.

The exception is thrown and caught within the try/catch block inside test() and prints out B. The finally block executes and prints out C.


Exception is propagated to main method.The exception which is thrown by finally block in test() method is passed to main method. see following code




Output is
B_B C


ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

SCJP1.5, SCWCD1.5
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi Bharat Makwana

It's because you are throwing an exception here

finally {
throw new Exception("C");
}

And finally is going to be executed in any case..

So that has to be declare or handle in main()

Hence the code

catch (Exception e) {
System.out.println("B_B " + e.getMessage());
}


will be executed
And you will get
"B_B " + "C"( for e.getMessage())

i.e. B_B C as output..

Regards..


rgrds,
Khushhal
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Please can anybody can ellaborate and explain the above given program,

how the main method does not throw exception?

Here the method is called in the try catch block as well as the method itself consist of of try catch block,so i am bit confused .
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi dhwani mathur

how the main method does not throw exception?


Because the rule is declare or handle..

Since you are properly handling the exception in main()
by using try and catch block..


You need not to declare them in main using throws keyword..

Regards..
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hi!!
Khushhal
Thank you for your explanation,i got the point.
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
hello Everyone,

Thanks a lot for the replies but I am still a bit confused about the test method.


When we are inside the test method then once the exception is thrown for the first time then it was caught by the subsequent catch clause but what happens to the exception which was thrown within the catch clause? So after that finally has to be executed, so it again throws the exception which we catching in the main() method. So the output is

B_B C

but again what happens to the exception which was thrown within the catch clause of test method. I thought it should be caught within the main method but indeed it is not.
Can you guys explain me why is that?

Regards
Padma
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Bharat Makwana:
Hi Keith,


Exception is propagated to main method.The exception which is thrown by finally block in test() method is passed to main method. see following code




Output is
B_B C


Oops. Sorry. You're right. I didn't read it close enough.
 
Don't get me started about those stupid light bulbs.
 
subject: Can somebody please explain me the output
 
Similar Threads
Q on Exception Handling
ExamLab Exception
Exception Handling
Casting Throwbale to Exception
Whizlabs Exception Handling Question