001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.exception.evaluator; 020 021 022/** 023 * The base class for resourcemanager exceptions thrown by REEF services, such as 024 * storage and networking routines. SERVICES that throw exceptions that 025 * applications may be able to cope with should subclass ServiceRuntimeException 026 * or ServiceException. 027 * 028 * @see ServiceException which is generally preferred over ServiceRuntimeException. 029 */ 030public class ServiceRuntimeException extends RuntimeException { 031 private static final long serialVersionUID = 1L; 032 private final boolean isWrappedServiceException; 033 034 public ServiceRuntimeException() { 035 this.isWrappedServiceException = false; 036 } 037 038 /** 039 * It often is the case that analogous ServiceException and ServiceRuntimeExceptions 040 * are needed so that exception types can be uniformly thrown from Reef APIs that 041 * declare throws clauses, and legacy interfaces that do not. This constructor 042 * wraps ServiceExceptions, and is the preferred way to deal with such situations. 043 * 044 * @param cause ServiceException to wrap 045 */ 046 public ServiceRuntimeException(final ServiceException cause) { 047 super("Wrapped ServiceException", cause); 048 this.isWrappedServiceException = true; 049 } 050 051 public ServiceRuntimeException(final String message, final Throwable cause) { 052 super(message, cause); 053 this.isWrappedServiceException = false; 054 } 055 056 public ServiceRuntimeException(final String message) { 057 super(message); 058 this.isWrappedServiceException = false; 059 060 } 061 062 public ServiceRuntimeException(final Throwable cause) { 063 super(cause); 064 this.isWrappedServiceException = cause instanceof ServiceException; 065 } 066 067 /** 068 * Upon catching a ServiceRuntimeException, the receiving code should call unwrap(). 069 * 070 * @return this, or getCause(), depending on whether or not this is a wrapped ServiceException. 071 */ 072 public Throwable unwrap() { 073 return this.isWrappedServiceException ? getCause() : this; 074 } 075}