I receive an error in my Android Studio after calling a method. I'm more curious if the error is what I think it is or not. The error first tells about an unexpected response code 500 after wich it shuts down and gives a fatal exception.
I've been trying to get the exception that comes from the fatal exception but no luck. To my understanding, I cannot catch the unexpected response code 500.
The error
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
E/Volley: [1668] BasicNetwork.performRequest: Unexpected response code 500 for (website)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.geotask, PID: 14594
java.lang.IllegalStateException
at com.example.geotask.PSHandler$1.onErrorResponse(PSHandler.java:48)
at com.android.volley.Request.deliverError(Request.java:617)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:104)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Application terminated.
The code that gives the error
try {
psHandler.addNewUser(name, LoginActivity.this, new RetrievedCallback<Void>() {
@Override
public void execute(Void input) {
Intent intent;
intent = new Intent(LoginActivity.this, MainActivity.class);
Toast.makeText(LoginActivity.this, "New account created", Toast.LENGTH_SHORT).show();
startActivity(intent);
finish();
}
});
} catch (IllegalStateException e) {
}
I removed the website it refers to. As you can see I'm trying to catch the possible error that is given in the logcat, but I'm not sure if I'm capable. I would like to know if I'm capable of preventing the error from the logcat.
The reason why you cannot catch the exception is because you are trying to catch it outside the method where it actually happens:
try {
psHandler.addNewUser(name, LoginActivity.this, new RetrievedCallback<Void>() {
@Override
public void execute(Void input) {
throw new IllegalStateException();
}
});
} catch (IllegalStateException e) {
// This line will never be reached because the exception
// is not thrown here, but inside the callback method
}
Instead, you should add the try/catch
inside the callback method:
psHandler.addNewUser(name, LoginActivity.this, new RetrievedCallback<Void>() {
@Override
public void execute(Void input) {
try {
throw new IllegalStateException();
} catch (IllegalStateException e) {
// This line will be reached
}
}
});