diff --git a/src/main/java/org/usb4java/LibUsbException.java b/src/main/java/org/usb4java/LibUsbException.java new file mode 100644 index 0000000..fb881f5 --- /dev/null +++ b/src/main/java/org/usb4java/LibUsbException.java @@ -0,0 +1,60 @@ +/* + * Copyright 2014 Klaus Reimer + * See LICENSE.md for licensing information. + */ + +package org.usb4java; + +/** + * A runtime exception which automatically outputs the libusb error string. + * + * @author Klaus Reimer (k@ailis.de) + */ +public final class LibUsbException extends RuntimeException +{ + /** Serial version UID. */ + private static final long serialVersionUID = 1L; + + /** The libusb error code. */ + private final int errorCode; + + /** + * Constructs a libusb exception which just outputs the error code and + * the error message from libusb. + * + * @param errorCode + * The error code. + */ + public LibUsbException(final int errorCode) + { + super(String.format("USB error %d: %s", -errorCode, + LibUsb.strError(errorCode))); + this.errorCode = errorCode; + } + + /** + * Constructs a libusb exception which outputs the error code and + * the error message from libusb together with a custom error message. + * + * @param message + * The error message. + * @param errorCode + * The error code. + */ + public LibUsbException(final String message, final int errorCode) + { + super(String.format("USB error %d: %s: %s", -errorCode, message, + LibUsb.strError(errorCode))); + this.errorCode = errorCode; + } + + /** + * Returns the error code. + * + * @return The error code + */ + public int getErrorCode() + { + return this.errorCode; + } +}