1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.imaging; 18 19 import java.io.IOException; 20 21 /** 22 * The root class for implementing custom exceptions in the Apache Commons Imaging component. 23 */ 24 public class ImagingException extends IOException { 25 26 private static final long serialVersionUID = -1L; 27 28 static String getType(final Object value) { 29 if (value == null) { 30 return "null"; 31 } 32 if (value instanceof Object[]) { 33 return "[Object[]: " + ((Object[]) value).length + "]"; 34 } 35 if (value instanceof char[]) { 36 return "[char[]: " + ((char[]) value).length + "]"; 37 } 38 if (value instanceof byte[]) { 39 return "[byte[]: " + ((byte[]) value).length + "]"; 40 } 41 if (value instanceof short[]) { 42 return "[short[]: " + ((short[]) value).length + "]"; 43 } 44 if (value instanceof int[]) { 45 return "[int[]: " + ((int[]) value).length + "]"; 46 } 47 if (value instanceof long[]) { 48 return "[long[]: " + ((long[]) value).length + "]"; 49 } 50 if (value instanceof float[]) { 51 return "[float[]: " + ((float[]) value).length + "]"; 52 } 53 if (value instanceof double[]) { 54 return "[double[]: " + ((double[]) value).length + "]"; 55 } 56 if (value instanceof boolean[]) { 57 return "[boolean[]: " + ((boolean[]) value).length + "]"; 58 } 59 return value.getClass().getName(); 60 } 61 62 /** 63 * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently be initialized by a call to 64 * {@link #initCause}. 65 * 66 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method. 67 */ 68 public ImagingException(final String message) { 69 super(message); 70 } 71 72 /** 73 * Constructs a new exception with the specified detail message and cause. 74 * <p> 75 * Note that the detail message associated with {@code cause} is <em>not</em> automatically incorporated in this exception's detail message. 76 * </p> 77 * 78 * @param message the detail message (which is saved for later retrieval by the {@link #getMessage()} method). 79 * @param data data typed to build the message. 80 */ 81 public ImagingException(final String message, final Object data) { 82 super(message + ": " + data + " (" + getType(data) + ")"); 83 } 84 85 /** 86 * Constructs a new exception with the specified detail message and cause. 87 * <p> 88 * Note that the detail message associated with {@code cause} is <em>not</em> automatically incorporated in this exception's detail message. 89 * </p> 90 * 91 * @param message the detail message (which is saved for later retrieval by the {@link #getMessage()} method). 92 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that 93 * the cause is nonexistent or unknown.) 94 */ 95 public ImagingException(final String message, final Throwable cause) { 96 super(message, cause); 97 } 98 99 /** 100 * Constructs a new exception. 101 * 102 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that the 103 * cause is nonexistent or unknown.) 104 */ 105 public ImagingException(final Throwable cause) { 106 super(cause); 107 } 108 }