aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception.." Watch "Exception.." New topic
Author

Exception..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi, I got it from
http://www.go4java.20m.com/mock1.htm

public class exception
{
public static void main(String args[])
{
System.out.println("A");
try
{
return; // if i comment this i am getting AC as output
}
catch(Exception e)
{
System.out.println("B");
}

System.out.println("C");

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

thanks in advance
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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
Greenhorn

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!

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

SCJP 5 (100%)
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
thanks...
 
 
subject: Exception..