home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Exceptions.java)

This example Java source code file (Exceptions.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

error, exceptions, runtimeexception, throwable, unhandledcheckeduserexception

The Exceptions.java Java example source code

 * Copyright (C) 2010 Google Inc.
 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package com.google.inject.internal;

 * Rethrows user-code exceptions in wrapped exceptions so that Errors can target the correct
 * exception.
 * @author sameb@google.com (Sam Berlin)
class Exceptions {

   * Rethrows the exception (or it's cause, if it has one) directly if possible.
   * If it was a checked exception, this wraps the exception in a stack trace
   * with no frames, so that the exception is shown immediately with no frames
   * above it.
  public static RuntimeException rethrowCause(Throwable throwable) {
    Throwable cause = throwable;
    if(cause.getCause() != null) {
      cause = cause.getCause();
    return rethrow(cause);
  /** Rethrows the exception. */
  public static RuntimeException rethrow(Throwable throwable) {    
    if(throwable instanceof RuntimeException) {
      throw (RuntimeException)throwable;
    } else if(throwable instanceof Error) {
      throw (Error)throwable;
    } else {
      throw new UnhandledCheckedUserException(throwable);

   * A marker exception class that we look for in order to unwrap the exception
   * into the user exception, to provide a cleaner stack trace.
  static class UnhandledCheckedUserException extends RuntimeException {
    public UnhandledCheckedUserException(Throwable cause) {

Other Java examples (source code examples)

Here is a short list of links related to this Java Exceptions.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.