Factory method for usage during variable or field initialisation.

@param exception exception to throw
@param <T>       Supplied object type
@param <E>       RuntimeException subtype
@return the exception supplier
@throws E the provided exception
507928
public static <T, E extends RuntimeException> ExceptionSupplier<T, E> throwA(E exception) {
        return new ExceptionSupplier<>(exception);
    }
593406
public static <X extends Throwable> X wrap(@Nullable Throwable e, @Nonnull ExWF<X> exceptionFactory) {
        handleErrors(e); 
        return exceptionFactory.produce(e); //NOSONAR
    }
694257
public static RuntimeException launderEntryProcessorException(Exception e) {
      if (e instanceof CacheException)
         return (CacheException) e;

      return new EntryProcessorException(e);
   }
1219294
static <T> Supplier<T> supplier(Callable<T> callable) {
    return () -> {
      try {
        return callable.call();
      } catch (Exception e) {
        throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
      }
    };
  }
801813
public ErrorPage getErrorPageByExceptionType(Throwable rootException) {
        // Begin 256281
        // Check for a perfect match first
        String exceptionName = rootException.getClass().getName();
        Object obj = exceptionErrorPages.get(exceptionName);
        if (obj != null)
            return (ErrorPage) obj;
        // Check to see if its a child of another exception type in the list
        Iterator i = exceptionErrorPages.values().iterator();
        ErrorPage curEP = null;
        ClassLoader warClassLoader = getWebApp().getClassLoader();
        while (i.hasNext()) {
            ErrorPage ep = (ErrorPage) i.next();
            // Class exceptionClass = ep.getException();
            Class exceptionClass = ep.getException(warClassLoader);
            if (exceptionClass != null && exceptionClass.isInstance(rootException)) {
                if (curEP == null)
                    curEP = ep;
                // else if
                // (curEP.getException().isAssignableFrom(exceptionClass))
                // //PK52168
                else if (curEP.getException(warClassLoader).isAssignableFrom(exceptionClass)) // PK52168
                    curEP = ep;
            }
        }
        return curEP;
        // End 256281
    }
1261987
public final RuntimeException processDoMethodInvokeException (Exception e, Object object, Object [] argumentArray) {
//        if (e instanceof IllegalArgumentException) {
//            //TODO: test if this is OK with new MOP, should be changed!
//            // we don't want the exception being unwrapped if it is a IllegalArgumentException
//            // but in the case it is for example a IllegalThreadStateException, we want the unwrapping
//            // from the runtime
//            //Note: the reason we want unwrapping sometimes and sometimes not is that the method
//            // invocation tries to invoke the method with and then reacts with type transformation
//            // if the invocation failed here. This is OK for IllegalArgumentException, but it is
//            // possible that a Reflector will be used to execute the call and then an Exception from inside
//            // the method is not wrapped in a InvocationTargetException and we will end here.
//            boolean setReason = e.getClass() != IllegalArgumentException.class || this instanceof org.codehaus.groovy.reflection.GeneratedMetaMethod;
//            return MetaClassHelper.createExceptionText("failed to invoke method: ", this, object, argumentArray, e, setReason);
//        }

        if (e instanceof RuntimeException)
          return (RuntimeException) e;

        return MetaClassHelper.createExceptionText("failed to invoke method: ", this, object, argumentArray, e, true);
    }
1084770
public static <T, U, X extends RuntimeException> U convertBean(T source, Class<U> targetClass,
            Supplier<X> exceptionSupplier) {
        Objects.requireNonNull(targetClass,"TargetClass can not be null");
        if (Objects.isNull(source)) {
            return SupplierUtil.ifNonNullThrowOrElse(exceptionSupplier, () -> null);
        }
        Handler handler = ConverterContext.getActionHandler(source.getClass(), targetClass);
        log.info("Call method \"{}\"", handler.getMethod());
        try {
            return targetClass.cast(handler.getMethod().invoke(handler.getHandler(), source));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw ConvertException.of("Call method \"" + handler.getMethod() + "\" failed", e);
        }
    }
900811
public static String getExceptionMessage(Throwable exception)
    {
        StringWriter        out = new StringWriter();
        exception.printStackTrace(new PrintWriter(out));
        BufferedReader      in = new BufferedReader(new StringReader(out.toString()));
        try
        {
            StringBuilder       str = new StringBuilder();
            for(;;)
            {
                String  line = in.readLine();
                if ( line == null )
                {
                    break;
                }
                if ( str.length() > 0 )
                {
                    str.append(", ");
                }
                str.append(line);
            }
            return str.toString();
        }
        catch ( IOException e )
        {
            // ignore
        }
        return "n/a";
    }
793122
private void lifeCycleExceptionHandler(Throwable t, EJBModuleMetaDataImpl mmd) // F743-14982
    {
        if (t instanceof RuntimeException) {
            // Is the RuntimeException an application exception?
            RuntimeException rtex = (RuntimeException) t;
            if (mmd.getApplicationExceptionRollback(rtex) != null) // F743-14982
            {
                // Yes it is, which is not valid for lifecycle callback
                // methods.  So turn this into a system runtime exception.
                InterceptorProxy w = ivInterceptorProxies[ivNextIndex - 1];
                String lifeCycle = w.getMethodGenericString();
                EJBException ex = ExceptionUtil.EJBException(lifeCycle
                                                             + " is not allowed to throw an application exception", rtex);
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "lifeCycleExceptionHandler throwing EJBException", ex);
                }
                throw ex;
            } else {
                // Not an application exception, so let the mapping
                // strategy handle this system runtime exception.
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "lifeCycleExceptionHandler is rethrowing RuntimeException "
                                 + "from lifecycle callback method: " + t,
                             t);
                }
                throw rtex;
            }
        } else if (t instanceof Error) {
            // Let the mapping strategy handle this unchecked exception.
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "lifeCycleExceptionHandler is rethrowing Error from "
                             + "lifecycle callback method: " + t,
                         t);
            }
            throw (Error) t;
        } else {
            // Must be a checked exception, which should never happen since
            // InterceptorMetaDataFactory throws EJBConfigurationException
            // and does a Tr.error using message key of INVALID_LIFECYCLE_SIGNATURE_CNTR0231E
            // if interceptor method throws clause is not empty. But if
            // it does happen, wrap the exception in a special EJBException
            // that does not include EJB container in the exception stack.
            InterceptorProxy w = ivInterceptorProxies[ivNextIndex - 1]; // d367572.7
            String lifeCycle = w.getMethodGenericString(); // d367572.7
            EJBException ex = ExceptionUtil.EJBException(lifeCycle
                                                         + " is not allowed to throw a checked exception", t);
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) // d367572.7
            {
                Tr.debug(tc, "lifeCycleExceptionHandler throwing EJBException", ex);
            }
            throw ex;
        }
    }
295907
private static void throwRuntimeException(String msg, InvocationTargetException e) {
    Throwable cause = e.getCause();
    if (cause != null) {
      throw new RuntimeException(msg + ": " + cause.getMessage(), cause);
    } else {
      throw new RuntimeException(msg + ": " + e.getMessage(), e);
    }
  }
432110
public static RuntimeException asRuntime(Throwable e) {
		if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		} else {
			return new WrappedAsRuntimeException(e);
		}
	}
299471
public static String formatException(Throwable exception) {
		StringBuilder buffer = new StringBuilder();

		formatException(exception, buffer);

		return buffer.toString();
	}
717200
public static  RuntimeException throwCastException(ClassCastException ex,GroupTemplate gt){
		String clsName = gt.getConf().getEngine();
		if(clsName.equals("org.beetl.core.engine.DefaultTemplateEngine")){
			return ex;
		}else{
			String detail = ex.getMessage();
			detail = detail + "如果采用优化引擎，会假定传给模板的变量是同一类型,如果不是，请使用directive dynamic 变量；来避免";
			throw new RuntimeException(detail,ex);
		}
	}
994268
public static RuntimeException asRuntimeException(Exception e) {
        if (e instanceof RuntimeException) {
            return (RuntimeException) e;
        }
        return UnexpectedMethodInvocationException.triage(e);
    }
299821
public static void exception (TraceComponent tc, Exception e) {
    exception(null, tc, (Throwable)e);
  }
959245
public static <Return> Return retryUntilNoException(Callable<Return> callable, int maxTry, Class<? extends Throwable> exceptionClass) throws Exception {
        String errMsg = "连续重试失败" + maxTry + "次。";
        Exception t = null;
        while (maxTry > 0) {
            try {
                return callable.call();
            } catch (Exception throwable) {
                if (exceptionClass.isAssignableFrom(throwable.getClass())) {
                    //只对指定的异常重试，其他异常直接抛出去
                    maxTry--;
                    t = throwable;
                } else {
                    throw throwable;
                }
            }
        }
        LOG.error(new RuntimeException(errMsg, t));
        //这里是对重试N次后依然抛出指定异常的情况，原样抛出
        throw t;
    }
56923
private static Throwable getInnerException(Throwable exception) {// NOPMD

		final Throwable tmpException = exception;

		exception = getExceptionCauseUsingWellKnownTypes(tmpException);
		if (exception == null) {
			for (CauseMethodName methodName : CauseMethodName.values()) {
				exception = getExceptionCauseUsingMethodName(tmpException, methodName);
				if (exception != null) {
					break;
				}
			}
		}

		return exception;
	}