Example usage for java.lang Throwable initCause

List of usage examples for java.lang Throwable initCause

Introduction

In this page you can find the example usage for java.lang Throwable initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:Main.java

public static void main(String[] argv) {
    Throwable t = new Exception("from java2s.com");

    t.initCause(new Throwable(""));

}

From source file:com.buaa.cfs.utils.NetUtils.java

@SuppressWarnings("unchecked")
private static <T extends IOException> T wrapWithMessage(T exception, String msg) {
    Class<? extends Throwable> clazz = exception.getClass();
    try {// w  w  w.  j a v  a2 s .  c o m
        Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
        Throwable t = ctor.newInstance(msg);
        return (T) (t.initCause(exception));
    } catch (Throwable e) {
        LOG.warn("Unable to wrap exception of type " + clazz + ": it has no (String) constructor", e);
        return exception;
    }
}

From source file:com.wolvereness.overmapped.OverMapped.java

private void process() throws Throwable {
    validateInput();/*from  www. j  a v  a  2  s  .c o m*/

    final MultiProcessor executor = MultiProcessor.newMultiProcessor(cores - 1,
            new ThreadFactoryBuilder().setDaemon(true)
                    .setNameFormat(OverMapped.class.getName() + "-processor-%d")
                    .setUncaughtExceptionHandler(this).build());
    final Future<?> fileCopy = executor.submit(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            if (original != null) {
                if (original.exists()) {
                    original.delete();
                }
                Files.copy(input, original);
            }
            return null;
        }
    });
    final Future<Iterable<?>> mappings = executor.submit(new Callable<Iterable<?>>() {
        @Override
        public Iterable<?> call() throws Exception {
            final Object yaml = new Yaml().load(Files.toString(maps, Charset.forName("UTF8")));
            if (yaml instanceof Iterable)
                return (Iterable<?>) yaml;
            if (yaml instanceof Map)
                return ImmutableList.of(yaml);
            throw new ClassCastException(String.format("%s (%s) implements neither %s nor %s", yaml,
                    yaml == null ? Object.class : yaml.getClass(), Iterable.class, Map.class));
        }
    });

    final Map<String, ByteClass> byteClasses = newLinkedHashMap();
    final List<Pair<ZipEntry, byte[]>> fileEntries = newArrayList();

    readClasses(executor, byteClasses, fileEntries);

    try {
        reorderEntries(byteClasses);
    } catch (final CircularOrderException ex) {
        final Throwable throwable = new MojoFailureException("Circular class hiearchy detected");
        throwable.initCause(ex);
        throw throwable;
    }

    final Multimap<String, String> depends = processDepends(byteClasses);
    final Multimap<String, String> rdepends = processReverseDepends(depends);

    final BiMap<String, String> nameMaps = HashBiMap.create(byteClasses.size());
    final BiMap<String, String> inverseNameMaps = nameMaps.inverse();

    final BiMap<Signature, Signature> signatureMaps = HashBiMap.create();
    final BiMap<Signature, Signature> inverseSignatureMaps = signatureMaps.inverse();

    final Map<Signature, Integer> flags = newHashMap();

    final Remapper inverseMapper = new Remapper() {
        @Override
        public String map(final String typeName) {
            final String name = inverseNameMaps.get(typeName);
            if (name != null)
                return name;
            return typeName;
        }
    };

    prepareSignatures(byteClasses, rdepends, nameMaps, signatureMaps);

    final Signature.MutableSignature signature = Signature.newMutableSignature("", "", "");
    final Set<String> searchCache = newHashSet();

    for (final Object mapping : mappings.get()) {
        final Map<?, ?> map = (Map<?, ?>) mapping;

        for (final SubRoutine subRoutine : SubRoutine.SUB_ROUTINES) {
            try {
                subRoutine.invoke(this, byteClasses, depends, rdepends, nameMaps, inverseNameMaps,
                        signatureMaps, inverseSignatureMaps, inverseMapper, signature, searchCache, flags, map);
            } catch (final Exception ex) {
                final Throwable throwable = new MojoFailureException("Failed to parse mappings in " + mapping);
                throwable.initCause(ex);
                throw throwable;
            }
        }
    }

    try {
        fileCopy.get();
    } catch (final ExecutionException ex) {
        throw new MojoFailureException(String.format("Could not copy `%s' to `%s'", input, original));
    }

    writeToFile(executor, byteClasses, fileEntries, nameMaps, signatureMaps, flags);

    executor.shutdown();

    final Pair<Thread, Throwable> uncaught = this.uncaught;
    if (uncaught != null)
        throw new MojoExecutionException(String.format("Uncaught exception in %s", uncaught.getLeft()),
                uncaught.getRight());
}

From source file:org.apache.jackrabbit.oak.plugins.document.persistentCache.BroadcastTest.java

private static void throwBoth(AssertionError e, AssertionError e2) throws AssertionError {
    Throwable ex = e;
    while (ex.getCause() != null) {
        ex = ex.getCause();//from www .j av a2s. c  om
    }
    ex.initCause(e2);
    throw e;
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Wrap the given reflection exception as a runtime exception.
 *//*from  w  w w. j  a v  a2s .  co  m*/
private static RuntimeException wrapReflectionException(Throwable t, Message message) {
    if (t instanceof InvocationTargetException)
        t = ((InvocationTargetException) t).getTargetException();
    t.initCause(new IllegalArgumentException(message.getMessage()));
    if (t instanceof RuntimeException)
        return (RuntimeException) t;
    return new GeneralException(t);
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

private static Throwable findThrowableByMessage(final Class<? extends Throwable> clazz, final String message,
        final Throwable cause) {
    try {//from   w  w w  .j a  v a 2 s .  c  o  m
        Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor(String.class);
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Throwable t = constructor.newInstance(message);
        if (cause != null) {
            t.initCause(cause);
        }
        return t;

    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR
            | InvocationTargetException e) { // NOSONAR
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable by message failed. Class [" + clazz.getName()
                    + "] has no suitable constructor: " + e.getMessage() + " (" + e.getClass().getName()
                    + ").");
        }
        return null;
    }
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

private static Throwable findThrowableByDefaultConstructor(final Class<? extends Throwable> clazz,
        final String message, final Throwable cause) {
    try {// w ww  .j av a2s.  c o  m
        Constructor<? extends Throwable> constructor = clazz.getDeclaredConstructor();
        if (!constructor.isAccessible()) {
            constructor.setAccessible(true);
        }
        Throwable t = constructor.newInstance();
        putMessage(t, message);
        if (cause != null) {
            t.initCause(cause); // NOSONAR
        }
        return t;

    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException // NOSONAR
            | InvocationTargetException e) { // NOSONAR
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable by default constructor failed. Class [" + clazz.getName()
                    + "] has no such constructor: " + e.getMessage() + " (" + e.getClass().getName() + ").");
        }
        return null;
    }
}

From source file:org.lilyproject.avro.AvroConverter.java

private void restoreCauses(List<AvroExceptionCause> remoteCauses, Throwable throwable) {
    Throwable causes = restoreCauses(remoteCauses);
    if (causes != null) {
        throwable.initCause(causes);
    }//from   w w w. j  a v a2s.c  o m
}

From source file:org.lilyproject.avro.AvroConverter.java

private Throwable restoreCauses(List<AvroExceptionCause> remoteCauses) {
    Throwable result = null;

    for (AvroExceptionCause remoteCause : remoteCauses) {
        List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(
                remoteCause.getStackTrace().size());

        for (AvroStackTraceElement el : remoteCause.getStackTrace()) {
            stackTrace.add(new StackTraceElement(el.getClassName(), el.getMethodName(), el.getFileName(),
                    el.getLineNumber()));
        }// ww  w  .  j a va  2 s .  c  om

        RestoredException cause = new RestoredException(remoteCause.getMessage(), remoteCause.getClassName(),
                stackTrace);

        if (result == null) {
            result = cause;
        } else {
            result.initCause(cause);
            result = cause;
        }
    }

    return result;
}