diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 4ab3af2..7c0f359 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -295,8 +295,9 @@ public final class SerialPort * * @param portDescriptor The desired serial port to use with this library. * @return A SerialPort object. + * @exception SerialPortInvalidPortException If a {@link SerialPort} object cannot be created due to a logical or formatting error in the portDescriptor parameter. */ - static public SerialPort getCommPort(String portDescriptor) + static public SerialPort getCommPort(String portDescriptor) throws SerialPortInvalidPortException { // Correct port descriptor, if needed try @@ -315,21 +316,13 @@ public final class SerialPort else if (!((new File(portDescriptor)).exists())) portDescriptor = "/dev/" + portDescriptor.substring(portDescriptor.lastIndexOf('/')+1); } - catch (Exception e) - { - SerialPort serialPort = new SerialPort(); - serialPort.comPort = "/dev/null"; - serialPort.friendlyName = "Bad Port"; - serialPort.portDescription = "Bad Port"; - return serialPort; - } + catch (Exception e) { throw new SerialPortInvalidPortException("Unable to create a serial port object from the invalid port descriptor: " + portDescriptor, e); } - // Create SerialPort object + // Create the SerialPort object SerialPort serialPort = new SerialPort(); serialPort.comPort = portDescriptor; serialPort.friendlyName = "User-Specified Port"; serialPort.portDescription = "User-Specified Port"; - return serialPort; } diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortInvalidPortException.java b/src/main/java/com/fazecast/jSerialComm/SerialPortInvalidPortException.java new file mode 100644 index 0000000..2e22256 --- /dev/null +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortInvalidPortException.java @@ -0,0 +1,81 @@ +/* + * SerialPortInvalidPortException.java + * + * Created on: Apr 15, 2019 + * Last Updated on: Apr 15, 2019 + * Author: Will Hedgecock + * + * Copyright (C) 2019-2019 Fazecast, Inc. + * + * This file is part of jSerialComm. + * + * jSerialComm is free software: you can redistribute it and/or modify + * it under the terms of either the Apache Software License, version 2, or + * the GNU Lesser General Public License as published by the Free Software + * Foundation, version 3 or above. + * + * jSerialComm is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of both the GNU Lesser General Public + * License and the Apache Software License along with jSerialComm. If not, + * see and . + */ + +package com.fazecast.jSerialComm; + +/** + * This class describes a serial port invalid port exception. + * + * @author Will Hedgecock <will.hedgecock@fazecast.com> + * @version 2.5.1 + * @see java.lang.RuntimeException + */ +public final class SerialPortInvalidPortException extends RuntimeException +{ + private static final long serialVersionUID = 3420177672598538224L; + + /** + * Constructs a {@link SerialPortInvalidPortException} with {@code null} as its error detail message. + */ + public SerialPortInvalidPortException() + { + super(); + } + + /** + * Constructs a {@link SerialPortInvalidPortException} with the specified detail message. + * + * @param message The detail message (which is saved for later retrieval by the {@link getMessage()} method). + */ + public SerialPortInvalidPortException(String message) + { + super(message); + } + + /** + * Constructs a {@link SerialPortInvalidPortException} with the specified detail message and cause. + *

+ * Note that the detail message associated with {@link cause} is not automatically incorporated into this exception's detail message. + * + * @param message message The detail message (which is saved for later retrieval by the {@link getMessage()} method). + * @param cause The cause (which is saved for later retrieval by the {@link getCause()} method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.) + */ + public SerialPortInvalidPortException(String message, Throwable cause) + { + super(message, cause); + } + + /** + * Constructs a {@link SerialPortInvalidPortException} with the specified cause and a detail message of {@code (cause==null ? null : cause.toString()) } + * (which typically contains the class and detail message of {@code cause}). This constructor is useful for exceptions that are little more + * than wrappers for other throwables. + * + * @param cause The cause (which is saved for later retrieval by the {@link getCause()} method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.) + */ + public SerialPortInvalidPortException(Throwable cause) + { + super(cause); + } +}