title: How to catch the Exception in Retrofit android
body: <ol>
  <li>I have defined the classes as below.</li>
  <li>I have used dagger with Retrofit here</li>
  </ol>
  
  <p><strong>What I am trying to do</strong>::</p>
  
  <p>I am trying to catch the <code>OfflineException</code> in the request below how to catch it in Main Activity properly.</p>
  
  <hr>
  
  <p><strong>RequestInterceptor.java</strong></p>
  
  <pre><code>public class RequestInterceptor implements Interceptor {
  
      ConnectivityManager connectivityManager;
      Application mApplication;
  
      @Inject
      public RequestInterceptor(Application mApplication) {
          this.mApplication = mApplication;
          connectivityManager = (ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
      }
  
      @Override
      public Response intercept(Chain chain) throws IOException {
          if (isConnected()) {
              throw new OfflineException();
          }
  
          Request.Builder r = chain.request().newBuilder();
          return chain.proceed(r.build());
      }
  
      protected boolean isConnected() {
          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
          return networkInfo != null &amp;&amp; networkInfo.isConnectedOrConnecting();
      }
  
      public class OfflineException extends IOException {
  
          @Override
          public String getMessage() {
              return mApplication.getResources().getString(R.string.app_no_connectivity);
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>NetModule.java</strong></p>
  
  <pre><code>import android.app.Application;
  import android.content.SharedPreferences;
  import android.preference.PreferenceManager;
  
  import com.google.gson.FieldNamingPolicy;
  import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  
  import java.util.concurrent.TimeUnit;
  
  import javax.inject.Singleton;
  
  import commons.LinksAndKeys;
  import dagger.Module;
  import dagger.Provides;
  import okhttp3.Cache;
  import okhttp3.OkHttpClient;
  import retrofitDagger.retrofitUtils.RequestInterceptor;
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  
  @Module
  public class NetModule {
  
      String mBaseUrl;
      Application mApplication;
  
      // Constructor needs one parameter to instantiate.
      public NetModule(String baseUrl, Application application) {
          this.mBaseUrl = baseUrl;
          this.mApplication = application;
      }
  
      // Dagger will only look for methods annotated with @Provides
      @Provides
      @Singleton
      // Application reference must come from AppModule.class
      SharedPreferences providesSharedPreferences(Application application) {
          return PreferenceManager.getDefaultSharedPreferences(application);
      }
  
      @Provides
      @Singleton
      Application providesApplication() {
          return mApplication;
      }
  
      @Provides
      @Singleton
      Cache provideOkHttpCache(Application application) {
          int cacheSize = 10 * 1024 * 1024; // 10 MiB
          Cache cache = new Cache(application.getCacheDir(), cacheSize);
          return cache;
      }
  
      @Provides
      @Singleton
      Gson provideGson() {
          GsonBuilder gsonBuilder = new GsonBuilder();
          gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
          return gsonBuilder.create();
      }
  
      @Provides
      @Singleton
      OkHttpClient provideOkHttpClient(Cache cache) {
          OkHttpClient.Builder client = new OkHttpClient.Builder();
          client.cache(cache);
          client.addInterceptor(new  RequestInterceptor(mApplication));
          client.readTimeout(LinksAndKeys.READ_TIMEOUT, TimeUnit.SECONDS);
          client.connectTimeout(LinksAndKeys.CONNECTION_TIMEOUT, TimeUnit.SECONDS);
          return client.build();
      }
  
      @Provides
      @Singleton
      Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
          Retrofit retrofit = new Retrofit.Builder()
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .baseUrl(mBaseUrl)
                  .client(okHttpClient)
                  .build();
          return retrofit;
      }
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Inject
      Retrofit retrofit;
      TextView textView;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ((App) getApplication()).getNetComponent().inject(this);
  
          //Create textview and findViewByID
          textView = (TextView) findViewById(R.id.textview_post);
          //Create a retrofit call object
          Call&lt;List&lt;Post&gt;&gt; posts = retrofit.create(Restapi.class).getPosts();
  
          //Enque the call
          posts.enqueue(new Callback&lt;List&lt;Post&gt;&gt;() {
              @Override
              public void onResponse(Call&lt;List&lt;Post&gt;&gt; call, Response&lt;List&lt;Post&gt;&gt; response) {
                 //Set the response to the textview
                  textView.setText(response.body().get(0).getBody());
  
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;Post&gt;&gt; call, Throwable t) {
                  //Set the error to the textview
                  textView.setText(t.toString());
              }
          });
      }
  }
  </code></pre>