Home arrow static arrow Java Programming [Archive] - return statement should put beyond try/catch clause??
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - return statement should put beyond try/catch clause??
This topic has 2 replies on 1 page.

Posts:122
Registered: 6/23/03
return statement should put beyond try/catch clause??  
Jun 15, 2004 11:00 PM



 
The return statement should put beyond the try/catch clause, is that correct? Well,
I tried to put inside the try block, but it will have compile error though.

public String getValue()
{
String value;
try
{
}
catch(...)
{
}
return value;
}

please advise. thanks!!
 

Posts:2,909
Registered: 13.8.2003
Re: return statement should put beyond try/catch clause??  
Jun 15, 2004 11:09 PM (reply 1 of 2)



 
When a method returns a value, you must make sure that even if an exception is thrown and caught a value will be returned (or just throw the exception out of the method).

You can put a return clause as the last thing in the try block and another return clause after the catch block (this is where we go if we catch an exception so you probably want to return null).
 

Posts:158
Registered: 3/26/02
Re: return statement should put beyond try/catch clause??  
Jun 16, 2004 3:23 AM (reply 2 of 2)



 
plus you can have more than one as follow:

if condition is true then
return value1
else
return value2
end if

return value3
 
This topic has 2 replies on 1 page.