Retrofit crashes when I throw an exception in an interceptor Retrofit crashes when I throw an exception in an interceptor android android

Retrofit crashes when I throw an exception in an interceptor


OkHttp will only catch the exception that happens in the Interceptor if it's an IOException. You can see the code that does this here, the relevant parts (simplified) are as below:

try {  Response response = getResponseWithInterceptorChain();} catch (IOException e) {  responseCallback.onFailure(RealCall.this, e);}

So if you change your code to the following, you'll get a callback in your onFailure method as expected:

val client = OkHttpClient.Builder()     // Add interceptor that throws     .addInterceptor { chain ->         throw IOException("test")     }     .build()