Finally is solving the issue..but else not..
public class Propagate {
public static void main(String[] args) {
String str = "";
System.out.println(reverse(str));
}
22 ->public static String reverse(String str){
String rev = "";
try{
if(str.length() == 0){
throw new StrNull();
}
for(int i=(str.length() - 1);i >= 0;i--){
rev = rev + str.charAt(i);
}
34 ->return rev;
}catch(StrNull e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
class StrNull extends Exception{
public void printStackTrace(){
System.out.println("String length is Zero..\n");
}
}
Can anyone please tell me wats the issue in this code ?in Line no 22(marked) its showing my method is not returning any string..but its returning in line 34....the issue is solving if i add a finally and return the string from there...
[932 byte] By [
profilemia] at [2007-11-27 10:38:21]

try {
result = doStuff();
}
catch (...) {
print
}
return result;
That's a bad idea.
You're defeating the purpose of the exception mechanism. If you caught an exception, then result didn't properly get set. The calling code needs to know that.
Either:
* Don't catch the exception. Let it bubble up to the caller.
* Wrap it in a more layer-appropriate exception and throw that.
* Actually handle it. Provide a valid default value, or retry.
jverda at 2007-7-28 18:53:53 >

> There is a compilation error: "This method must
> return a result of type String"
>
> return rev; must be after try...catch...
No.
You're stil missing the point.
If you do that then the caller has no way to know something went wrong. You should just not catch the exception in the first place, since you're not doing anything to deal with it.
jverda at 2007-7-28 18:53:53 >
