Firebase异常未被捕获

时间:2017-01-29 06:13:11

标签: java android exception firebase firebase-authentication

这是我现有的代码。

mAuth.createUserWithEmailAndPassword(registration.getEmail(), registration.getPassword())
                    .addOnSuccessListener(this, authResult -> {
                        Log.i("exception0", "here0");
                    })
                    .addOnFailureListener(this, exception -> {
                        if (exception instanceof FirebaseAuthWeakPasswordException) {
                            Log.i("exception1", "here");
                        } else if (exception instanceof FirebaseAuthInvalidCredentialsException) {
                            Log.i("exception2", "here1");
                        } else if (exception instanceof FirebaseAuthUserCollisionException) {
                            Log.i("exception3", "here2");
                        } else if (exception instanceof FirebaseAuthInvalidUserException) {
                            Log.i("exception4", "here3");
                        } else if (exception instanceof FirebaseAuthException) {
                            Log.i("exception5", "here4");
                        } else if (exception instanceof FirebaseException) {
                            FirebaseException firebaseException = (FirebaseException) exception;
                            Log.i("exception6", "here5" + firebaseException.getMessage());
                        } else {
                            Log.i("exception7", "here6");
                        }
                    });

我知道异常需要是一个弱密码异常,但是被捕获的异常是Firebase异常。

我甚至尝试过以下代码

if(!task.isSuccessful()) {
try {
    throw task.getException();
} catch(FirebaseAuthWeakPasswordException e) {
    mTxtPassword.setError(getString(R.string.error_weak_password));
    mTxtPassword.requestFocus();
} catch(FirebaseAuthInvalidCredentialsException e) {
    mTxtEmail.setError(getString(R.string.error_invalid_email));
    mTxtEmail.requestFocus();
} catch(FirebaseAuthUserCollisionException e) {
    mTxtEmail.setError(getString(R.string.error_user_exists));
    mTxtEmail.requestFocus();
} catch(Exception e) {
    Log.e(TAG, e.getMessage());
}
}

但它总会捕获最后一个广义异常而不是特定异常。

1 个答案:

答案 0 :(得分:0)

尝试这个:

if(e.getClass().equals(FirebaseAuthWeakPasswordException.class))