FileDocCategorySizeDatePackage
ExceptionWithContext.javaAPI DocAndroid 5.1 API4103Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.util

ExceptionWithContext

public class ExceptionWithContext extends RuntimeException
Exception which carries around structured context.

Fields Summary
private StringBuffer
context
{@code non-null;} human-oriented context of the exception
Constructors Summary
public ExceptionWithContext(String message)
Constructs an instance.

param
message human-oriented message

        this(message, null);
    
public ExceptionWithContext(Throwable cause)
Constructs an instance.

param
cause {@code null-ok;} exception that caused this one

        this(null, cause);
    
public ExceptionWithContext(String message, Throwable cause)
Constructs an instance.

param
message human-oriented message
param
cause {@code null-ok;} exception that caused this one

        super((message != null) ? message :
              (cause != null) ? cause.getMessage() : null,
              cause);

        if (cause instanceof ExceptionWithContext) {
            String ctx = ((ExceptionWithContext) cause).context.toString();
            context = new StringBuffer(ctx.length() + 200);
            context.append(ctx);
        } else {
            context = new StringBuffer(200);
        }
    
Methods Summary
public voidaddContext(java.lang.String str)
Adds a line of context to this instance.

param
str {@code non-null;} new context

        if (str == null) {
            throw new NullPointerException("str == null");
        }

        context.append(str);
        if (!str.endsWith("\n")) {
            context.append('\n");
        }
    
public java.lang.StringgetContext()
Gets the context.

return
{@code non-null;} the context

        return context.toString();
    
public voidprintContext(java.io.PrintWriter out)
Prints the message and context.

param
out {@code non-null;} where to print to

        out.println(getMessage());
        out.print(context);
    
public voidprintContext(java.io.PrintStream out)
Prints the message and context.

param
out {@code non-null;} where to print to

        out.println(getMessage());
        out.print(context);
    
public voidprintStackTrace(java.io.PrintStream out)
{@inheritDoc}

        super.printStackTrace(out);
        out.println(context);
    
public voidprintStackTrace(java.io.PrintWriter out)
{@inheritDoc}

        super.printStackTrace(out);
        out.println(context);
    
public static com.android.dexgen.util.ExceptionWithContextwithContext(java.lang.Throwable ex, java.lang.String str)
Augments the given exception with the given context, and return the result. The result is either the given exception if it was an {@link ExceptionWithContext}, or a newly-constructed exception if it was not.

param
ex {@code non-null;} the exception to augment
param
str {@code non-null;} context to add
return
{@code non-null;} an appropriate instance

        ExceptionWithContext ewc;

        if (ex instanceof ExceptionWithContext) {
            ewc = (ExceptionWithContext) ex;
        } else {
            ewc = new ExceptionWithContext(ex);
        }

        ewc.addContext(str);
        return ewc;