[英]Java exception not being caught?
I´ve got the following code in a class called UserRepository
, that throws an Exception: 我在名为
UserRepository
的类中有以下代码,该代码引发异常:
public void addUser(final User user) throws IllegalArgumentException {
final String encodedUserId = Email.encodeID(user.getEmail().getAddress());
firebaseUsersRef.child(encodedUserId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
throw new IllegalArgumentException(USER_ALREADY_EXISTS);
} else {
Firebase firebaseUserReference = firebaseRef.child(USERS_TABLE).child(encodedUserId);
firebaseUserReference.setValue(user);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
throw new IllegalArgumentException(FIREBASE_ERROR);
}
});
}
I am using it in RegisterActivity.java
, like this: 我在
RegisterActivity.java
使用它,如下所示:
public void saveUser(){
saveUserToDb();
}
private void saveUser(User user) {
boolean savedUser = false;
try {
userRepository.addUser(user);
savedUser = true;
} catch (IllegalArgumentException iae) {
if (iae.getMessage().equals(UserRepository.USER_ALREADY_EXISTS)) {
showToast(getResources().getString(R.string.user_already_exists));
} else if (iae.getMessage().equals(UserRepository.FIREBASE_ERROR)) {
showToast(getResources().getString(R.string.firebase_error));
}
} finally {
if (savedUser) {
showToast(getResources().getString(R.string.new_user_created));
}
}
}
So I am catching the exception, if happens. 因此,如果发生的话,我正在捕获异常。 However, for some reason, the exception is not being caught .
但是,由于某种原因,未捕获到异常 。
Any hint? 有什么提示吗?
Thanks. 谢谢。
EDIT : 编辑 :
Here´s my stacktrace: 这是我的堆栈跟踪:
04-27 09:56:34.984 4741-4741/app E/AndroidRuntime: FATAL EXCEPTION: main
Process: app, PID: 4741
java.lang.IllegalArgumentException: User already exists
at app.repository.UserRepository$1.onDataChange(UserRepository.java:33)
at com.firebase.client.Query$1.onDataChange(Query.java:144)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-27 09:57:39.773 4741-4747/app W/art: Suspending all threads took: 6.473ms
Implementing Exceptions and try catch is very heavy task and one should always avoid using try catch. 实现异常和try catch是一项非常繁重的任务,应始终避免使用try catch。 Instead, you can implement the same thing using custom interface and trigger the function according to your requirements.
取而代之的是,您可以使用自定义界面实现相同的功能,并根据需要触发功能。 Please see the sample code here:
请在此处查看示例代码:
public interface CustomInterface {
void handleResult(String response);
}
Now change your addUser method to: 现在将您的addUser方法更改为:
public void addUser(final User user, CustomInterface customInterface){
final String encodedUserId = Email.encodeID(user.getEmail().getAddress());
firebaseUsersRef.child(encodedUserId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
customInterface.handleResult(USER_ALREADY_EXISTS); // I assume this is String
} else {
Firebase firebaseUserReference = firebaseRef.child(USERS_TABLE).child(encodedUserId);
firebaseUserReference.setValue(user);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
customInterface.handleResult(FIREBASE_ERROR); // I assume this is String
}
});
}
Then finally, change your saveUser Method to: 最后,将您的saveUser方法更改为:
private void saveUser(User user) {
boolean savedUser = false;
userRepository.addUser(user, new CustomInterface() {
@Override
public void handleResult(String response) {
if (response.equals(UserRepository.USER_ALREADY_EXISTS)) {
showToast(getResources().getString(R.string.user_already_exists));
} else if (response.equals(UserRepository.FIREBASE_ERROR)) {
showToast(getResources().getString(R.string.firebase_error));
}
// And Do whatever you want to do, here in this method
}
});
}
I hope now you'll be able to understand this. 我希望您现在能够理解这一点。
Try like this: 尝试这样:
public void addUser(final User user) {
final String encodedUserId = Email.encodeID(user.getEmail().getAddress());
firebaseUsersRef.child(encodedUserId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.exists()) {
try {
throw new IllegalArgumentException(USER_ALREADY_EXISTS);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} else {
Firebase firebaseUserReference = firebaseRef.child(USERS_TABLE).child(encodedUserId);
firebaseUserReference.setValue(user);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
try {
throw new IllegalArgumentException(FIREBASE_ERROR);
} catch (IllegalArgumentException e) {
e.printStackTrace();;
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.