Example usage for java.lang NumberFormatException initCause

List of usage examples for java.lang NumberFormatException initCause

Introduction

In this page you can find the example usage for java.lang NumberFormatException 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:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java

@Override
public Object convert(Executor executor, ArgumentConverter parser, Type type, String value) {
    Class<?> cls = ReflectionUtils.toClass(type);
    if (ClassUtils.isPrimitiveWrapper(cls))
        cls = ClassUtils.wrapperToPrimitive(cls);

    if (cls.equals(boolean.class))
        return this.isTrue(executor, value);
    else if (cls.equals(char.class)) {
        if (value.length() > 0)
            throw new NumberFormatException("Character arguments cannot be longer than one characters");
        return value.charAt(0);
    }//from   w w w.  j a  v a2  s. co m

    // Get the locale of the user and get a number-format according to it.
    LocaleResolver resolver = TranslationManager.getResolver(executor);
    Locale locale;
    if (resolver != null)
        locale = resolver.getLocale();
    else
        locale = Locale.ENGLISH;

    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setGroupingUsed(true);

    // Parse the value.
    Number result;
    try {
        result = nf.parse(value);
    } catch (ParseException e) {
        NumberFormatException nfe = new NumberFormatException("Invalid number");
        nfe.initCause(e);
        throw nfe;
    }

    // Returns the value in the correct type.
    if (cls.equals(int.class))
        return result.intValue();
    else if (cls.equals(float.class))
        return result.floatValue();
    else if (cls.equals(double.class))
        return result.doubleValue();
    else if (cls.equals(byte.class))
        return result.byteValue();
    else if (cls.equals(short.class))
        return result.shortValue();
    else if (cls.equals(long.class))
        return result.longValue();

    throw new NumberFormatException("Unknown primitive type.");
}

From source file:org.nightlabs.base.ui.exceptionhandler.ExceptionHandlerRegistry.java

/**
 * Processes exceptionHandler extension-point elements.
 * For each element one instance of exceptionHandler.class is registered
 * in the {@link ExceptionHandlerRegistry}.
 * @param element// ww w . j  a v a 2  s.c  o  m
 */
@Override
public void processElement(IExtension extension, IConfigurationElement element) throws Exception {
    if (element.getName().toLowerCase().equals("exceptionhandler")) { //$NON-NLS-1$
        String targetType = element.getAttribute("targetType"); //$NON-NLS-1$

        IExceptionHandler handler = (IExceptionHandler) element.createExecutableExtension("class"); //$NON-NLS-1$
        if (!IExceptionHandler.class.isAssignableFrom(handler.getClass()))
            throw new IllegalArgumentException("Specified class for element exceptionHandler must implement " //$NON-NLS-1$
                    + IExceptionHandler.class.getName() + ". " + handler.getClass().getName() + " does not."); //$NON-NLS-1$ //$NON-NLS-2$

        int priority = -1; // default => will be changed into 500
        String priorityString = element.getAttribute("priority"); //$NON-NLS-1$
        if (priorityString != null && !"".equals(priorityString)) { //$NON-NLS-1$
            try {
                priority = Integer.parseInt(priorityString);
                if (priority < 0 || priority > 1000)
                    throw new NumberFormatException("Out of range!"); //$NON-NLS-1$
            } catch (NumberFormatException x) {
                NumberFormatException y = new NumberFormatException("priority=\"" + priorityString //$NON-NLS-1$
                        + "\" is not a valid integer in the range between 0 and 1000!"); //$NON-NLS-1$
                y.initCause(x);
                throw y;
            }
        }

        addExceptionHandler(targetType, handler, priority);
    } else {
        // wrong element according to schema, probably checked earlier
        throw new IllegalArgumentException(
                "Element " + element.getName() + " is not supported by extension-point " + EXTENSION_POINT_ID); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:org.simplx.args.MainArgs.java

@SuppressWarnings({ "TypeMayBeWeakened" })
private static NumberFormatException numException(NumberFormatException e, String option) {

    NumberFormatException ne = new NumberFormatException("-" + option + " " + e.getMessage());
    ne.initCause(e);
    return ne;//from w  w w .  j  a  va  2  s  . c o  m
}