user4849541
user4849541

Reputation:

Android Binding & MVVM : unreported Exception; must be caught

I'm trying to learn the databinding and the MVVM pattern but I got a problem with Exceptions.

My viewmodel has an exception :

    public void play(String move) throws Exception, ChessMoveException{
    String[] positions = move.split("\\s+");
    try {
        Position position1 = new Position(positions[0]);
        Position position2 = new Position(positions[1]);
        this.model.turn(position1, position2);
        if(this.model.getCurrentColor().equals(Color.WHITE)) {
            this.model.setCurrentColor(Color.BLACK);
            messageTurn.set(R.string.blackTurn);
        } else {
            this.model.setCurrentColor(Color.WHITE);
            messageTurn.set(R.string.whiteTurn);
        }
    } catch (Exception ex) {
        //TODO : Toast Exception
    }
}

I'm using this method play in my MainActivityEventHandlers :

public void onButtonPlayClicked(View v) throws Exception {
    Log.i("MESSAGE", "Play");
    try {  
        this.activity.binding.getViewModel().play(this.activity.binding.editTurn.getText().toString());
    } catch (Exception ex) {
        Log.i("EXCEPTION", "");
    }
}

In my activity_main.xml, I used the onClick propriety : android:onClick="@{eventHandlers.onButtonPlayClicked}"

But when I try to launch my code, here is the error : Error:(374, 43) error: unreported exception Exception; must be caught or declared to be thrown

When I click on it, it redirects me on the ActivityMainBinding.java class which is generated so I can't edit it to add my Exceptions on the onClick method :

public void onClick(android.view.View arg0) {
     this.value.onButtonPlayClicked(arg0);
}

Is there anything to do to fix this error ? Thanks

Upvotes: 2

Views: 263

Answers (1)

stkent
stkent

Reputation: 20138

Your method

public void onButtonPlayClicked(View v) throws Exception {
    Log.i("MESSAGE", "Play");
    try {  
        this.activity.binding.getViewModel().play(this.activity.binding.editTurn.getText().toString());
    } catch (Exception ex) {
        Log.i("EXCEPTION", "");
    }
}

declares that it may throw Exceptions, but also catches all Exceptions. I imagine that this is somehow confusing the data binding plugin when it generates the binding classes.

Upvotes: 2

Related Questions