Android 如何将Firebase异常获取到switch语句中

Android 如何将Firebase异常获取到switch语句中,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我目前有以下代码: if (!task.isSuccessful() && task.getException() != null) { FirebaseCrash.report(task.getException()); Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(SignUp.this, "Authentication failed: " + task.get

我目前有以下代码:

if (!task.isSuccessful() && task.getException() != null) {
   FirebaseCrash.report(task.getException());
   Log.w(TAG, "signInWithCredential", task.getException());
   Toast.makeText(SignUp.this, "Authentication failed: " + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
有时用户会得到一个
FirebaseAuthUserCollisionException
,我想在
switch
语句中检测它,如下所示:

switch(task.getException()){
    case /*I don't know what goes here*/:
       //Whatever
}
但正如你所读到的,我不知道在
案例中应该放什么。我在里面放什么?换句话说,
FirebaseAuthUserCollisionException
在哪里,我可以访问它
FirebaseAuthUserCollisionException
只是一个类,因此我目前无法对其执行任何操作。

尝试:

if(task.getException() instanceof FirebaseAuthUserCollisionException){
    //whatever
}

Firebase身份验证异常都源于,它有一个方法

因此:


如果其他异常是这个异常类的子类,那么它也会属于这个异常类。非常感谢,但是如何获得“FirebaseAuthUserCollisionException”<代码>案例“FirebaseAuthUserCollisionException”:
?开关案例无法在Java中测试类。如果需要开关盒,则需要与
getErrorCode()
进行比较。如果你想和类进行比较,你需要像@rafal说的那样嵌套
If
s。好吧,但是我如何比较类呢?如果这是真的,为什么在答案中包含
switch(e.getErrorCode())
?喜欢你的答案,顺便说一句,非常有用:)rafal的答案展示了如何比较异常类。如果这对你不起作用,显示你是如何做的代码(通过编辑你的问题)。弗兰克:包含错误代码字符串常量的标识符(例如,
error\u EMAIL\u已经在使用中了
),但它们似乎无法作为某个类的公共成员访问。我看起来不够努力吗?一位勤奋的开发人员在Firebase库中发现了这个问题。在我看来,没有简单而干净的方法来做你想做的事情。除了这里给出的答案之外,你也可以考虑。
if(task.getException() instanceof FirebaseAuthException){
    FirebaseAuthException e = (FirebaseAuthException)task.getException();
    switch (e.getErrorCode()) {
        ...
    }
}