diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java index 0b17e1d906b0550171b0f28446ffcfb0faa08ff5..e5c1751254c37ba7a2b860ec1df5adf48a4014fc 100644 --- a/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/CheckedExceptionTunnel.java @@ -38,10 +38,28 @@ public final class CheckedExceptionTunnel extends RuntimeException assert (checkedException != null && checkedException instanceof RuntimeException) == false; } + /** + * Convenience wrapper for {@link #wrapIfNecessary(Exception)}. If <var>throwable</var> is an + * {@link Error}, this method will not return but the error will be thrown. + * + * @param throwable The exception to represent by the return value. + * @return A {@link RuntimeException} representing the <var>throwable</var>. + * @throws Error If <var>throwable</var> is an {@link Error}. + */ + public final static RuntimeException wrapIfNecessary(final Throwable throwable) throws Error + { + if (throwable instanceof Error) + { + throw (Error) throwable; + } + return wrapIfNecessary((Exception) throwable); + } + /** * Returns a {@link RuntimeException} from an <var>exception</var>. If <var>exception</var> is - * already a {@link RuntimeException}, itself is returned, otherwise a - * {@link CheckedExceptionTunnel} with <var>exception</var> as checked exception argument. + * already a {@link RuntimeException}, itself is returned, otherwise an appropriate unchecked + * equivalent. If no unchecked equivalent exists, a {@link CheckedExceptionTunnel} is returned + * with <var>exception</var> as checked exception argument. * * @param exception The exception to represent by the return value. * @return A {@link RuntimeException} representing the <var>exception</var>. @@ -54,7 +72,11 @@ public final class CheckedExceptionTunnel extends RuntimeException } if (exception instanceof InterruptedException) { - return new StopException(exception); + return new StopException((InterruptedException) exception); + } + if (exception instanceof java.util.concurrent.TimeoutException) + { + return new TimeoutException((java.util.concurrent.TimeoutException) exception); } return new CheckedExceptionTunnel(exception); } diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java b/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java index 8024ee680d196174800f26d6235913769434ef84..0dbe61f18aaf673f2a6972fffe8fb7f49158d6fb 100644 --- a/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/StopException.java @@ -17,7 +17,8 @@ package ch.systemsx.cisd.common.exceptions; /** - * Exception that signals that whoever gets it should stop its current work. + * Exception that signals that whoever gets it should stop its current work. This is an unchecked + * equivalent to an {@link InterruptedException}. * <p> * This is usually triggered by interrupting the thread that the work package is processed in and * regularly checking with {@link #check()}. @@ -35,7 +36,7 @@ public final class StopException extends RuntimeException super(); } - public StopException(final Throwable cause) + public StopException(InterruptedException cause) { super(cause); } diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/TimeoutException.java b/common/source/java/ch/systemsx/cisd/common/exceptions/TimeoutException.java new file mode 100644 index 0000000000000000000000000000000000000000..8ae6fbb57c1834f0303c2eea9fe23cce64147c9c --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/TimeoutException.java @@ -0,0 +1,45 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * 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 ch.systemsx.cisd.common.exceptions; + +/** + * Exception thrown when a blocking operation times out. This is an unchecked equivalent of + * {@link java.util.concurrent.TimeoutException}, i.e. it is derived from {@link RuntimeException}. + * + * @author Bernd Rinn + */ +public class TimeoutException extends RuntimeException +{ + + private static final long serialVersionUID = 1L; + + public TimeoutException() + { + super(); + } + + public TimeoutException(String msg) + { + super(msg); + } + + public TimeoutException(java.util.concurrent.TimeoutException cause) + { + super(cause); + } + +} \ No newline at end of file