【发布时间】:2020-05-18 17:09:03
【问题描述】:
我正在尝试从数据库中获取信息。并检查用户名是否已经存在 我正在使用 google cloud firestore 数据库。 我将该方法声明为抛出我创建的异常。 当我尝试调用抛出异常时,它告诉我需要将其放入 try/catch 块中。 我想知道为什么,因为我做了类似的事情(但是和我的老师一起使用 sqlite 并且效果很好)
我的代码:
@Override
public void isUserExists(final String username, String email) throws UserExistsException {
Log.e(TAG, "isUserExists: in start of method" );
// gets the document reference
CollectionReference usersRef = db.collection(COLLECTION_NAME);
//Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.
Query query = usersRef.whereEqualTo(KEY_USERNAME, username);
//Executes the query and returns the results as a QuerySnapshot.
// QuerySnapshot = A QuerySnapshot contains the results of a query. It can contain zero or more DocumentSnapshot objects. (is an iterable)
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Log.e(TAG, "isUserExists: in the onComplete" );
//A DocumentSnapshot contains data read from a document in your Cloud Firestore database.
if (task.isSuccessful()) {
Log.e(TAG, "isUserExists: in the if statement" );
for (DocumentSnapshot item : task.getResult()) {
Log.e(TAG, "isUserExists: in for loop" );
//getString() = Returns the value of the field as a String.
String user = item.getString(KEY_USERNAME);
if (user.equals(username)) {
//this throw line will not compile
throw new UserExistsException();
}
}
}
}
});
}
我正在实现这个接口:
public interface IUser {
void isUserExists(String username, String email) throws UserExistsException;
void isPassesMatch(String password, String rePass) throws PasswordMismatchException;
void checkLength(String username, String password) throws PasswordLengthException, UserNameLengthException;
void checkUserCred(String username, String password) throws UserCredentialException;
void registerUser (String username, String password, String email) throws UserException;
}
我在课堂上和老师做过的类似的事情:
@Override
public void userExists() throws UserExistsException {
String sqlStatement = "SELECT * FROM " + TABLE_NAME + " WHERE userName = '" + this.userName + "'";
Cursor res = db.rawQuery(sqlStatement, null);
if(res.moveToFirst()){
throw new UserExistsException();
}
}
编辑(添加我抛出的异常):
public class UserExistsException extends UserException {
public UserExistsException(String message) {
super(message);
}
public UserExistsException() {
super("User Already Exists");
}
}
另一个编辑(添加处理方法及其异常的位置 ):
private void register() {
String uName = regActEtUname.getText().toString();
String uPass = regActEtPass.getText().toString();
String rePass = regActEtRePass.getText().toString();
String uEmail = regActEtEmail.getText().toString();
try {
// checks if the user exissts
utils.isUserExists(uName,uEmail);
//checks if the length is fulfilled
utils.checkLength(uName,uPass);
//checks if the passwords match
utils.isPassesMatch(uPass, rePass);
//registers the user
utils.registerUser(uName,uPass,uEmail);
} catch (UserExistsException | PasswordLengthException | UserNameLengthException | PasswordMismatchException e) {
Log.e("fbdb", "register: " + e.getMessage() );
TastyToast.makeText(context,e.getMessage(), TastyToast.LENGTH_LONG, TastyToast.ERROR);
} catch (UserException e) {
Log.e("regErr", "register: " + e.getMessage() );
} catch (Exception e) {
Log.e("regErr", "register: " + e.getMessage() );
}
}
【问题讨论】:
-
你在哪一行有问题?
-
问题有点不清楚,我会回答我所理解的,当你调用一个抛出异常的方法时,你要么需要捕获它并有意义地使用它,要么进一步抛出它。
-
@user2222 我在 isUserExist 方法内的最后一个
if块中遇到了问题。 @sunil.kms123 我将该方法声明为抛出异常。当我尝试投掷它时,它说我需要将投掷包裹在 try/catch 块中 -
我看到你把它扔进了 onComplete 方法而不是 isUserExists 方法;我认为您应该在 onComplete 方法中添加 try catch bock 来处理该异常,因为仅当被覆盖的方法也抛出相同的异常时,被覆盖的方法(在您的情况下为 onComplete)才能抛出异常。从java的角度来看,因为我对android知之甚少,所以可能有更好的解决方法。
-
你老师的例子很好,因为异常是在 userExists() 方法中抛出的
标签: java android exception google-cloud-firestore try-catch