benb
benb

Reputation: 351

Use Exceptions in if condition

Im using Firebase Auth in order to manage my login and register for the app.

I had like to add toast messages based on the exception that I receive.

For example, if the exception is from FirebaseAuthInvalidCredentialsException I had like to toast one message. If it is from FirebaseAuthUserCollisionException then I had like to use another toast.

I use something like:

auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    LayoutInflater inflater = LayoutInflater.from( SignUpActivity.this );
                    View toastview = inflater.inflate( R.layout.toast_registered, null );
                    Toast toast = new Toast( SignUpActivity.this );
                    toast.setView( toastview );
                    toast.setGravity( Gravity.CENTER, 0, 3 );
                    toast.setDuration( Toast.LENGTH_LONG );
                    toast.show();
                }

                if (!task.isSuccessful()) {
                    Toast.makeText(SignUpActivity.this, "toast1." + task.getException(),
                            Toast.LENGTH_SHORT).show();
                }

                if (!task.isSuccessful()) {
                    Toast.makeText(SignUpActivity.this, "toast2." + task.getException(),
                            Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(SignUpActivity.this, MainActivity.class));
                    finish();
                }
            }
        });

So basically I need to add something to the if condition however im not sure what.

I saw a use of catch but I dont think this is the situation.

Thank you

Upvotes: 2

Views: 163

Answers (2)

Luke
Luke

Reputation: 516

First of all you need only one condition like task.isSuccessful().

You can do something like that:

if(task.isSuccessful()) {
    try {
        throw task.getException();
    } catch(FirebaseAuthInvalidCredentialsException e) {
        /*Toast here*/
    } catch(FirebaseAuthUserCollisionException e) {
        /*Toast here*/
    } catch(...) ...
}

With

...
throw task.getException();
...

you will throw the specific exception and with

} catch(FirebaseAuthInvalidCredentialsException e) {
    /*Toast here*/
}

you will catch the specific exception to handle that and do what you want, in your case you will write code to show toast inside catch statement.

Remember to catch all exceptions or when unhandled exception is thrown your app will crash.

You can also handle generic exception:

if(task.isSuccessful()) {
    try {
        throw task.getException();
    } catch(FirebaseAuthInvalidCredentialsException e) {
        /*Toast here*/
    } catch(FirebaseAuthUserCollisionException e) {
        /*Toast here*/
    } catch(Exception e) {
        /*Handle generic exception*/
    }
}

Bye

Upvotes: 0

Thomas Cook
Thomas Cook

Reputation: 4853

This should do it:

auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    LayoutInflater inflater = LayoutInflater.from( SignUpActivity.this );
                    View toastview = inflater.inflate( R.layout.toast_registered, null );
                    Toast toast = new Toast( SignUpActivity.this );
                    toast.setView( toastview );
                    toast.setGravity( Gravity.CENTER, 0, 3 );
                    toast.setDuration( Toast.LENGTH_LONG );
                    toast.show();
                }
                else {
                    if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                         Toast.makeText(SignUpActivity.this, "toast1." + task.getException(),
                         Toast.LENGTH_SHORT).show();
                    }
                    else if (task.getException() instanceof FirebaseAuthUserCollisionException) {
                         Toast.makeText(SignUpActivity.this, "toast2." + task.getException(),
                            Toast.LENGTH_SHORT).show();
                    }   
                    startActivity(new Intent(SignUpActivity.this, MainActivity.class));
                    finish();
                }
            }
        });

So, you check if task was successful, if not you switch on error type and show corresponding toast before starting signup activity and finishing this activity.

Upvotes: 1

Related Questions