How do I check to see if a resource exists in Android

According to the javadoc you don’t need the try catch:
http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

if getIdentifier() returns zero, it means that no such resource exists.
Also 0 – is an illegal resource id.

So your result boolean variable is equivalent to (test != 0).

Anyway your try/finally is bad, because all it does it set the result variable to false even if exception is thrown from the body of try: mContext.get..... and then it just “rethrows” the exception after getting out of finally clause. And I suppose that is not what you want to do in case of exception.

Leave a Comment