1

I know this type of questions have come before, and I have went threw them, but didn't get the final part which I need to do.

My ExceptionClass

public class ProException extends Exception {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public ProException(String message) {
    super(message);
  }
}

My ActivityClass (Custom Adapter in Android for ListView)

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    try {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.pause_client_trigger_request_select_sku_list_row, null);
            tvItemName = (TextView) vi.findViewById(R.id.list_row_ItemName);                
        } else {
            tvItemName = (TextView) vi.findViewById(R.id.list_row_ItemName);
        }

        hmap = new HashMap<String, String>();
        hmap = data.get(position);
        tvItemName.setText(hmap.get(KEY_ItemName));

    } catch (ProException ex) {
        Log.i(Tag, ex.getMessage());
    }

    return vi;
}

Now what I want is.

If there is an Exception which occurs in this try catch any Exception. It should be captured by my custom class (ProException). But its not allowing. Any help

Message in Java Eclipse Editor Unreachable catch block for ProException. This exception is never thrown from the try statement body

7
  • 2
    you need to throw a ProException in order to catch it... Commented Jun 3, 2013 at 10:21
  • throw but How, I need to catch any exception which happens here. Commented Jun 3, 2013 at 10:22
  • @MDMalik I think you need to extend some Exception class which is being provided by Android and not an Exception(main java class). Commented Jun 3, 2013 at 10:23
  • @Ketan, All I want is my custom Exception class to handle any Exception which occurs. can't that be done. So that I could just ask my exception class to perform any action I require Commented Jun 3, 2013 at 10:27
  • @MDMalik see developer.android.com/reference/java/lang/Exception.html are recoverable exception handler being provided for android. If you think any of such exception will occur in your code. And you want your own implementation for same. You can extend that specific class instead Exception. Commented Jun 3, 2013 at 10:31

4 Answers 4

1

All those views don't know about your custom exception class. You have to extend/write your own View classes that throw your custom exception, or manually throw an exception within the try block.

throw new ProException();

Sign up to request clarification or add additional context in comments.

2 Comments

you mean. catch (Exception ex) {throw new ProException();} Is this what you mean, catch a general exception and throw it?
That would work although you would then need to surround that with a try/catch block too. You would basically rethrow any exception as your custom exception. Personally I think this would be too messy. I'm not sure what the benefit is so not sure what else to suggest.
0

MD please tell which call in the method getView can throw ProException. It seems none of the code in that method can throw ProException so you are getting "This exception is never thrown from the try statement body" and block not reachable. In order to use in try catch it should get the Proexception or you can catch Exception in try catch of getView and then wrap it in ProException.

Please see if this solves the issue.

Comments

0

ProException is custom exception. when code block throw exception then it cann't able to catch because when you write any custom exception then hierarchy become

Exception->>ProException 

if code block throw any exception, It will try to find out catch block of Exception instead of ProException. OR in simple term It will try to find out type of exception

So you have to catch Exception instead of ProException

Custom exception helpful whenever you throw somewhere it.

Comments

0

I think what you want is

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    try {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.pause_client_trigger_request_select_sku_list_row, null);
            tvItemName = (TextView) vi.findViewById(R.id.list_row_ItemName);                
        } else {
            tvItemName = (TextView) vi.findViewById(R.id.list_row_ItemName);
        }

        hmap = new HashMap<String, String>();
        hmap = data.get(position);
        tvItemName.setText(hmap.get(KEY_ItemName));

    } catch (Exception ex) {
        Log.i(Tag, ex.getMessage());
        throw new ProException(ex.getMessage());
    }

    return vi;
}

Then the calling code can catch ProException. You should consider taking the causing exception as a constructor argument to ProException (then you can pass it to super(cause, message)).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.