Reputation: 51
The following code is supposed to return the last value in arraylist if it has one, if not, how could I return the exception?
public int getLast() {
try {
return arrl.get(arrl.size() - 1);
} catch (Exception e) {
System.out.println("Caught "+ e);
}
}
arrl here is ArrayList
I excuted above code and received "error: missing return statement"
Upvotes: 3
Views: 157
Reputation: 2312
There is the possability that your method can not return a value (for what reasons ever). In this case you should use now java.lang.Optional (Java-8) as return type:
public Optional<Integer> getLast() {
try {
return Optional<T>.ofNullable(arrl.get(arrl.size() - 1));
} catch (Exception e) {
e.printStackTrace();
return Optional.empty();
}
}
This way you will force the user of your method to deal with the situation of not having a result to be returned. (BTW: you should not catch Exception, mostly never. Instead take care that arrl is not null and arrl.size() is within the limits. Handling exceptions is for the... umm exeptional cases, not to be used as control statement.)
Upvotes: 0
Reputation: 310860
There's really no good reason for the try/catch
block here at all. Just let the NPE or AIOOBE be thrown to the caller and let him deal with them.
Upvotes: 2
Reputation: 4360
You have return statement in your try{}
but not in your catch(){}
, this gives you the exception. You can include the return in both or return after the try catch.
public int getLast() {
Object o=null;
try {
o=arrl.get(arrl.size() - 1);
} catch (Exception e) {
System.out.println("Caught "+ e);
}
return o;
}
Also to return the exception back to the caller you need to rethrow it from your method, or not catch it at all
catch (Exception e) {
System.out.println("Caught "+ e);
throw e;
}
or
public int getLast() {
return arrl.get(arrl.size() - 1);
}
But I doubt any exception being thrown from this statement except for IndexOutOfBoundsException
Upvotes: 3
Reputation: 18646
You are getting this error because if the first return
in the try
-part didn't work the compiler doesn't know what it should return instead. You need either another return
or exception rethrow in the catch
-part:
public int getLast() {
try {
return arrl.get(arrl.size() - 1);
} catch (Exception e) {
System.out.println("Caught "+ e);
// You need to return something here or rethrow the e...
}
// or return something here.
}
...but this kind of logic is bad any way and this function shouldn't throw exceptions at all. I'd be better to check it the array has any items before getting the last one. If you run this in a loop the performace might be really bad if the error is thrown frequently.
public int getLast() {
if (arrl != null && arrl.size() > 0) {
return arrl.get(arrl.size() - 1);
}
else {
return -1;
}
}
or
public Integer getLast() {
if (arrl != null && arrl.size() > 0) {
return arrl.get(arrl.size() - 1);
}
else {
return null;
}
}
Upvotes: 1
Reputation: 31290
In this scenario, the only exceptions that may be raised are NPE due to arrl being null and an indexing error if the list is empty. If the class is properly initialised, the NPE shouldn't happen at all. The other exception is a client error, which may be passed on to the caller. Alternatively, one might consider returning null (after changing int to Integer):
public Integer getLast() {
try {
return arrl.get(arrl.size() - 1);
} catch (IndexOutOfBoundsException ioobe) {
return null;
}
}
Upvotes: 0
Reputation: 4476
You don't return an exception.. You throw it back to the caller.
public int getLast() {
try {
return arrl.get(arrl.size() - 1);
} catch (Exception e) {
System.out.println("Caught "+ e);
throw e;
}
}
Your code gives you compilation error because you are not returning anything in catch block. throwing would be good enough.
Alternatively, don't catch it, any uncaught exceptions will be automatically thrown back to the caller. So below code would also throw the exception back to the calling method.
public int getLast() throws Exception {
return arrl.get(arrl.size() - 1);
}
Upvotes: 2