Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception.." Watch "Exception.." New topic


Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi, I got it from

public class exception
public static void main(String args[])
return; // if i comment this i am getting AC as output
catch(Exception e)


here 'c' is not reached,why? and can we return something in void main method?

thanks in advance
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

c would have reached if it was in finally block. Since it is not, so return in try will end the execution of the method.

you can use a simple return statement in void methods i.e. return; you cannot return any value like return 0;//error

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Hemnathbabu Kottaiveedu

Joined: Mar 13, 2008
Posts: 28
Hi Preetha,

There are two forms of return statements that can be used depending on whether the method is a void or non void method.
return Statement
Form of return Statement In void Method In Non-void Method

return; optional not allowed

return <expression>; not allowed mandatory

So c is not reached because the flow of execution is transfered to the point from where the main() method has been called.

Hope this helps you!

[ November 20, 2008: Message edited by: Hemnathbabu Kottaiveedu ]

SCJP 5 (100%)
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
I agree. Here's the link:
subject: Exception..
It's not a secret anymore!