org.ops4j.dadl.exc.Exceptions Maven / Gradle / Ivy
/*
* Copyright 2015 OPS4J Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ops4j.dadl.exc;
/**
* Helper class for rethrowing a checked exception as runtime exception.
*
* Usage:
*
*
* try {
* // ...
* }
* catch (IOException exc) {
* throw Exceptions.unchecked(exc);
* }
*
*
* @author hwellmann
*
*/
public class Exceptions {
/**
* Hidden constructor.
*/
private Exceptions() {
}
/**
* Rethrows a checked exception as unchecked exception. This method tricks the compiler into
* thinking the exception is unchecked, rather than wrapping the given exception in a new
* {@code RuntimeException}.
*
* This method never returns. Nevertheless, it specifies a return type so it can be invoked as
* {@code throw unchecked(exc)} in contexts where an exception type is syntactically required
* (e.g. when the enclosing method is non-void).
*
* @param exc
* checked exception
* @return syntactically, a runtime exception (but never actually returns)
*/
public static RuntimeException unchecked(Exception exc) {
Exceptions. adapt(exc);
return null;
}
@SuppressWarnings("unchecked")
private static void adapt(Exception exc) throws T {
throw (T) exc;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy