java - try ... catch 没有捕获 IllegalArgumentException

标签 java android

open-source Camera App中,我在以下代码中发生崩溃

try {
    // crash happens in next line:
    PtpUsbConnection connection = new  PtpUsbConnection(usbManager.openDevice(device), in, out,
                      device.getVendorId(), device.getProductId());
    camera = new EosCamera(connection, listener, new WorkerNotifier(context));
}
catch (IllegalArgumentException e) {
    Log.i(TAG, "IllegalArgumentException: camera was switched off? "+ e.getMessage());
    e.printStackTrace();
    return false;
}
catch (Exception e) {
    Log.i(TAG, "Exception: camera was switched off? "+ e.getMessage());
    e.printStackTrace();
    return false;
}

崩溃产生以下日志

02-25 10:41:14.698    9238-9238/com.remoteyourcam.usb E/UsbManager﹕ exception in UsbManager.openDevice
java.lang.IllegalArgumentException: device /dev/bus/usb/001/008 does not exist or is restricted
        at android.os.Parcel.readException(Parcel.java:1469)
        at android.os.Parcel.readException(Parcel.java:1419)
        at android.hardware.usb.IUsbManager$Stub$Proxy.openDevice(IUsbManager.java:373)
        at android.hardware.usb.UsbManager.openDevice(UsbManager.java:308)
        at com.remoteyourcam.usb.ptp.PtpUsbService.connect(PtpUsbService.java:238)
        at com.remoteyourcam.usb.ptp.PtpUsbService.initialize(PtpUsbService.java:135)
        at com.remoteyourcam.usb.MainActivity.onStart(MainActivity.java:232)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
        at android.app.Activity.performStart(Activity.java:5413)
        at android.app.Activity.performRestart(Activity.java:5469)
        at android.app.Activity.performResume(Activity.java:5474)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2945)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2984)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
        at dalvik.system.NativeStart.main(Native Method)

有一系列可重现的事件导致崩溃。具体来说:当应用程序(显然)没有预见到这种可能性时,在 USB 总线上切换相机

我希望简单地捕获这个异常然后忽略它。

但是,即使代码显式捕获 IllegalArgumentException 以及(为了更好地衡量)一般 Exception,应用程序仍然会继续崩溃。

为什么我的 IllegalArgumentException 没有被捕获?

----更新:

openDevice() 方法是 Android 库的一部分。它实际上似乎可以处理Exception

这是我的导入

package com.remoteyourcam.usb.ptp;

import java.util.Map;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.util.Log;

import com.remoteyourcam.usb.AppConfig;
import com.remoteyourcam.usb.ptp.Camera.CameraListener;
import com.remoteyourcam.usb.ptp.PtpCamera.State;

----更新2:

这是我在 Android 18 中看到的 openDevice() 实现。据我所知,它处理一般的 Exception

/**
 * Opens the device so it can be used to send and receive
 * data using {@link android.hardware.usb.UsbRequest}.
 *
 * @param device the device to open
 * @return a {@link UsbDeviceConnection}, or {@code null} if open failed
 */
public UsbDeviceConnection openDevice(UsbDevice device) {
    try {
        String deviceName = device.getDeviceName();
        ParcelFileDescriptor pfd = mService.openDevice(deviceName);
        if (pfd != null) {
            UsbDeviceConnection connection = new UsbDeviceConnection(device);
            boolean result = connection.open(deviceName, pfd);
            pfd.close();
            if (result) {
                return connection;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "exception in UsbManager.openDevice", e);
    }
    return null;
}

最佳答案

异常发生在另一个线程中。

您正在异步初始化并将工作分派(dispatch)到另一个线程。您的代码仅捕获将 Parcel 分派(dispatch)到远程服务时的异常。

有关 Android 中远程过程调用的更多信息,您可能需要阅读 Deep Dive into Binder .

关于java - try ... catch 没有捕获 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28716005/

相关文章:

java - 存储在 Heroku 上运行的基于 Java 的 REST 服务的图像

java - 使用 hibernate 进行 Birt 报告

java - 无法在Android studio中运行Scratch文件

android - 如何让 ViewModel 观察者最初不观察?

java - 如何修复Android Studio中的 ‘null json data ’错误

Android相机 Intent 在拍摄肖像时保存图像风景

java - 反转多维数组

java - Java程序中的SQL查询连接问题

java - 通过 JaxB JXC 为 XSD 序列生成数组

android - Android 上的 reCAPTCHA Ionic/cordova