title: How to catch SecurityException for location permissions
body: <p>I'm getting the following crash on certain devices API 6.0+:</p>
  
  <pre><code>Fatal Exception: java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
         at android.os.Parcel.readException(Parcel.java:1693)
         at android.os.Parcel.readException(Parcel.java:1646)
         at com.google.android.gms.internal.zzed.zza(Unknown Source)
         at com.google.android.gms.internal.zzcda.zzdw(Unknown Source)
         at com.google.android.gms.internal.zzcdd.zzvQ(Unknown Source)
         at com.google.android.gms.internal.zzcdj.zzvQ(Unknown Source)
         at com.google.android.gms.location.zzf.zza(Unknown Source)
         at com.google.android.gms.internal.zzbaq.zza(Unknown Source)
         at com.google.android.gms.internal.zzbdd.zzb(Unknown Source)
         at com.google.android.gms.internal.zzbdd.zzqq(Unknown Source)
         at com.google.android.gms.internal.zzbdd.onConnected(Unknown Source)
         at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
         at com.google.android.gms.common.internal.zzn.zzj(Unknown Source)
         at com.google.android.gms.common.internal.zze.zzrj(Unknown Source)
         at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
         at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:154)
         at android.os.HandlerThread.run(HandlerThread.java:61)
  </code></pre>
  
  <p>Note that <strong>I am asking the user for location permission before making any location related operation</strong>. But for some reason I can't reproduce, some devices seem to be reporting having location permission when I use:</p>
  
  <pre><code>boolean hasPermission = ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
  </code></pre>
  
  <p>Because of that, I need to be able to catch the <code>SecurityException</code> and ignore it when it happens. Or perhaps figure out a way to tell the user to enable location permissions manually. </p>
  
  <p>The crash above started to happen when I started to use the new APIs from the Play Services to request location using the <code>FusedLocationProviderClient</code>. It crashes when I try to do the following without location permissions grated, for example:</p>
  
  <p><code>LocationServices.getFusedLocationProviderClient(this).getLocationAvailability().addOnFailureListener(myListener);</code></p>
  
  <p>The failure listener does not send the crash to <code>myListener</code> unfortunately. Surrounding the above with <code>try..catch</code> doesn't work either. I.e., <strong>this doesn't work</strong> and the app crashes anyway throwing <code>SecurityException</code>:</p>
  
  <pre><code>try {
      LocationServices.getFusedLocationProviderClient(this)
          .getLocationAvailability()
          .addOnFailureListener(myListener);
  } catch(Exception e) {
      Log.e(e);
  }
  </code></pre>
  
  <p>This problem seems to be related to the following issues:</p>
  
  <ul>
  <li><a href="https://productforums.google.com/forum/#!topic/play/xox8L1H-4oQ" rel="noreferrer">SecurityException in Android Play Services GoogleApiAvailability class</a></li>
  <li><a href="https://issuetracker.google.com/issues/35829071" rel="noreferrer">Bug: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.</a> This one is marked as fixed but the problem seems to be still be present.</li>
  </ul>
  
  <p>Any ideas?</p>