CannotCloseException.java

  1. /*******************************************************************************
  2.  * Copyright 2012 André Rouél
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  ******************************************************************************/
  16. package net.sf.uadetector.exception;

  17. /**
  18.  * Thrown to indicate that a {@link java.io.Closeable} cannot be closed.<br>
  19.  * <br>
  20.  * This exception is intended to tunnel the checked exception {@link java.io.IOException} during the call
  21.  * {@link java.io.Closeable#close()}.
  22.  *
  23.  * @author André Rouél
  24.  */
  25. public class CannotCloseException extends RuntimeException {

  26.     private static final long serialVersionUID = -8641033043995976022L;

  27.     protected static final String DEFAULT_MESSAGE = "Cannot close the given Closeable.";

  28.     protected static final String MESSAGE_WITH_INFO = "Cannot close the given Closeable: %s";

  29.     private static String format(final String url) {
  30.         return String.format(MESSAGE_WITH_INFO, url);
  31.     }

  32.     /**
  33.      * Constructs an {@code CannotCloseException} with the default message {@link CannotCloseException#DEFAULT_MESSAGE}.
  34.      */
  35.     public CannotCloseException() {
  36.         super(DEFAULT_MESSAGE);
  37.     }

  38.     /**
  39.      * Constructs an {@code CannotCloseException} with the message {@link CannotCloseException#MESSAGE_WITH_INFO}
  40.      * including additional information.
  41.      *
  42.      * @param info
  43.      *            additional information why a {@link java.io.Closeable} cannot be closed
  44.      */
  45.     public CannotCloseException(final String info) {
  46.         super(format(info));
  47.     }

  48.     /**
  49.      * Constructs a new exception with the message {@link CannotCloseException#MESSAGE_WITH_INFO} including additional
  50.      * information.
  51.      *
  52.      * @param info
  53.      *            additional information why a {@link java.io.Closeable} cannot be closed
  54.      * @param cause
  55.      *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
  56.      *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
  57.      */
  58.     public CannotCloseException(final String info, final Throwable cause) {
  59.         super(format(info), cause);
  60.     }

  61.     /**
  62.      * Constructs a new exception with the default message {@link CannotCloseException#DEFAULT_MESSAGE}.
  63.      *
  64.      * @param cause
  65.      *            the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
  66.      *            {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
  67.      */
  68.     public CannotCloseException(final Throwable cause) {
  69.         super(DEFAULT_MESSAGE, cause);
  70.     }

  71. }