英文:
Try to handle firebaseauth with customexception
问题
在我的Android项目中,我尝试在 FirebaseAuth 的 reauthenticate.onFailure 块中抛出自定义异常,以处理用户输入的错误密码。但是它不起作用。IDE 表示我应该将 throw 语句放在 try 和 catch 块中,但那样也不起作用。请问是否有可能以某种方式实现这一点?
public void verifyUserPassword(String password) throws WrongPasswordException {
AuthCredential credential = EmailAuthProvider.getCredential(user.getEmail(), password);
user.reauthenticate(credential).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String errorCode = ((FirebaseAuthException) e).getErrorCode();
if (errorCode.equals("ERROR_WRONG_PASSWORD")) {
throw new WrongPasswordException("Wrong password");
}
}
});
}
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String password = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(password)) {
Toast.makeText(getContext(), "Field can not be empty", Toast.LENGTH_SHORT).show();
return;
}
try {
accountListener.verifyUserPassword(etPassword.getText().toString());
profileChangeListener.onProfileAddOpen();
getDialog().dismiss();
} catch (WrongPasswordException e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
}
});
英文:
in my android project I try to throw a custom exception in the FirebaseAuth reauthenticate.onFailure block to handle wrong password input from user. But it doesn't work. The IDE says I should put the throw statement in a try and catch block, but it doesn't work either. Please can someone tell me if this is even possible or not in some way?
public void verifyUserPassword(String password) throws WrongPasswordException {
AuthCredential credential = EmailAuthProvider.getCredential(user.getEmail(), password);
user.reauthenticate(credential).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String errorCode = ((FirebaseAuthException) e).getErrorCode();
if (errorCode.equals("ERROR_WRONG_PASSWORD")) {
throw new WrongPasswordException("Wrong password");
}
}
});
}
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String password = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(password)) {
Toast.makeText(getContext(), "Field can not be empty", Toast.LENGTH_SHORT).show();
return;
}
try {
accountListener.verifyUserPassword(etPassword.getText().toString());
profileChangeListener.onProfileAddOpen();
getDialog().dismiss();
} catch (WrongPasswordException e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
}
});
答案1
得分: 1
OnFailureListener有一个方法
onFailure(@NonNull Exception e)
您需要重写这个特定的方法以提供您的实现。然而,您不允许从此方法中抛出异常,因为方法签名中没有throws
关键字。
因此,您将无法从此方法实现中抛出已检查异常,但是没有人可以阻止您从此方法中抛出RuntimeException,因为它们不是已检查的异常。如果适用于您的情况,您可以像这样做:
public class WrongPasswordException extends RuntimeException{
}
随意进行编辑或注释,因为这可能会帮助其他人。
英文:
OnFailureListener has method
onFailure(@NonNull Exception e)
And you need to override this particular method to provide your implementation. However you are not allowed to throw Exceptions from this method because there is no throws
in the method signature.
So You will not be able to throw Checked Exceptions from within this method implementation however no one can stop you from throwing RuntimeException from this method as they are not checked Exceptions. So You can do something like this if it fits your scenarios.
public class WrongPasswordException extends RuntimeException{
}
Feel free to edit or comment as it may help someone else.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论