アンドロイドでキャッチ例外

Iliz:

私は、メソッドを呼び出した後、私のAndroid Studioでエラーが発生します。エラーが、私はそれがあるか考えるものである場合、私はもっと興味があります。エラーは最初、それがシャットダウンし、致命的な例外を与えるウィッヒ後に予期しない応答コード500について伝えます。

私は致命的な例外が、運から来ている例外を取得しようとしてきました。私の理解するために、私は予期しない応答コード500をキャッチすることはできません。

エラー

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.

エラーを与えるコード

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) {
}

私はそれが参照するWebサイトを削除しました。あなたが見ることができるように私はlogcatに与えられている可能性のあるエラーをキャッチしようとしているが、私は可能だかはわかりません。私はlogcatからエラーを防止することが可能なんですかどうかを知りたいと思います。

フリオ・E.ロドリゲスキャビン:

あなたはそれが実際に起こるの方法の外にそれをキャッチしようとしているので、あなたが例外をキャッチすることができない理由は次のとおりです。

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
}

代わりに、あなたが追加する必要がありtry/catch、コールバックメソッド内:

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
        }
    }
});

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=119794&siteId=1
おすすめ