开发者

Catch exception

I got Activity, in onCreate() I try to fetch data:

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        ...............
        fillUI();
    }

 public void getDetailedItem(){
        FeedParser parser=new FeedParser();
        try{
           mItem=parser.parseDetailed();
        }catch(Exception e){
            closeAndShowError();
        }
    }

    public void fillUI(){
        getDetailedItem();
        if(mItem!=null){
        ...............
        }else{
          closeAndShowError();
        }
    }

private void closeAndShowError(){
        Context context = getApplicationContext();
        CharSequence text = getResources().getString(R.string.toast_error);
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

        fin开发者_如何转开发ish();
    }

Them problem is, when Exception occures, it is caught, and I expect my activity to finish itself, but I still get code inside fillUI() executed, so I had to put if statement after getDetailedItem(); How do I solve it?


You can do something like this:

public void getDetailedItem(){
        FeedParser parser=new FeedParser();
        try{
           mItem=parser.parseDetailed();
        }catch(Exception e){
           mItem = null;
        }
}

Then in fillUI() if mItem is null you show's error;


You could pass the exception that might get thrown in parseDetailed to the fillUI method:

public void getDetailedItem() throws Exception {
    FeedParser parser=new FeedParser();
    mItem=parser.parseDetailed();
}

public void fillUI(){
    try {
        getDetailedItem();
        // rest of your code..
    } catch(Exception e) {
        closeAndShowError();
    }
}


The finish() statement is not an immediate return, your code continues to run until it decides it's completed. You need to leave yourself an indicator of the exception, and return up through your call chain when it happens, instead of continuing to run.


Since you are catching the Exception; the fillUI() function is never notified of any error.

Your codepath is as following: fillUI -> getDetailedItem -> exception caught -> closeAndShowError -> finish() -> return to closeAndShowError -> return to getDetailedItem after caught block -> return back to fillUI.

So basically, the code after getDetailedItem in fillUI is called because you caught the exception and didn't let the other function know. Either by return value, or by throwing (and maybe catching) a new exception.


finish() does cause the Activity to stop as the next step in its lifecycle. But at the point you call it, it's still in the process of another lifecycle step: onCreate(). It will not exit, somehow, immediately in the middle of a method.

That's the answer, but, better would be to redesign your code a bit. This is probably not the cleanest way to handle it if you're dealing with questions like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜