Example usage for java.lang Throwable Throwable

List of usage examples for java.lang Throwable Throwable

Introduction

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

Prototype

public Throwable(Throwable cause) 

Source Link

Document

Constructs a new throwable with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static int parseInt(String string, int radix) throws Throwable {
    if (radix >= 2 && radix <= 36) {
        if (string == null) {
            throw invalidInt(string);
        } else {/*from w w  w  . java2  s. com*/
            int length = string.length();
            int i = 0;
            if (length == 0) {
                throw invalidInt(string);
            } else {
                boolean negative = string.charAt(i) == 45;
                if (negative) {
                    ++i;
                    if (i == length) {
                        throw invalidInt(string);
                    }
                }

                return parseInt(string, i, radix, negative);
            }
        }
    } else {
        throw new Throwable("Invalid radix: " + radix);
    }
}

From source file:Main.java

private static long parseLong(String string, int offset, int radix, boolean negative) throws Throwable {
    long max = -9223372036854775808L / (long) radix;
    long result = 0L;

    long next;//from ww w .j  a  va 2s.  co m
    for (long length = (long) string.length(); (long) offset < length; result = next) {
        int digit = digit(string.charAt(offset++), radix);
        if (digit == -1) {
            throw new Throwable("Invalid long: \"" + string + "\"");
        }

        if (max > result) {
            throw new Throwable("Invalid long: \"" + string + "\"");
        }

        next = result * (long) radix - (long) digit;
        if (next > result) {
            throw new Throwable("Invalid long: \"" + string + "\"");
        }
    }

    if (!negative) {
        result = -result;
        if (result < 0L) {
            throw new Throwable("Invalid long: \"" + string + "\"");
        }
    }

    return result;
}

From source file:fi.jumi.core.api.StackTraceTest.java

@Test
public void has_same_message_as_original_exception() {
    Throwable original = new Throwable("original message");

    StackTrace copy = StackTrace.from(original);

    assertThat(copy.getMessage(), is("original message"));
}

From source file:dkpro.similarity.experiments.wordchoice.util.WordChoiceProblemFactory.java

public static List<WCProblem> getWordChoiceProblems(JCas jcas) throws AnalysisEngineProcessException {

    List<WCProblem> wcpList = new ArrayList<WCProblem>();

    for (WordChoiceProblem wcp : JCasUtil.select(jcas, WordChoiceProblem.class)) {
        // get the sentences => they represent target and candidates
        List<Sentence> candidateList = JCasUtil.selectCovered(jcas, Sentence.class, wcp);

        if (candidateList.size() != 5) {
            throw new AnalysisEngineProcessException(new Throwable(
                    "Expected five sentence annotations, but only " + candidateList.size() + " were found."));
        }//from   ww  w. j  a  v a2  s.  co m

        Sentence target = candidateList.get(0);

        List<Lemma> targetAnnotationList = JCasUtil.selectCovered(jcas, Lemma.class, target);
        if (targetAnnotationList.size() != 1) {
            System.out.println("'" + target.getCoveredText() + "'");
            throw new AnalysisEngineProcessException(new Throwable(targetAnnotationList.size()
                    + " Lemma annotations for the target found, but expected only one."));
        }
        String targetString = targetAnnotationList.get(0).getValue();

        String cand1 = getCandidate(jcas, candidateList.get(1));
        String cand2 = getCandidate(jcas, candidateList.get(2));
        String cand3 = getCandidate(jcas, candidateList.get(3));
        String cand4 = getCandidate(jcas, candidateList.get(4));

        List<String> candList1 = getCandidateList(jcas, candidateList.get(1));
        List<String> candList2 = getCandidateList(jcas, candidateList.get(2));
        List<String> candList3 = getCandidateList(jcas, candidateList.get(3));
        List<String> candList4 = getCandidateList(jcas, candidateList.get(4));

        wcpList.add(new WCProblem(targetString, cand1, cand2, cand3, cand4, candList1, candList2, candList3,
                candList4, wcp.getCorrectAnswer()));

    }

    return wcpList;
}

From source file:fi.jumi.core.api.StackTraceTest.java

@Test
public void has_same_toString_as_original_exception() {
    Throwable original = new Throwable("original message");

    StackTrace copy = StackTrace.from(original);

    assertThat(copy.toString(), is(original.toString()));
}

From source file:dinamica.ChartOutput.java

public void print(GenericTransaction t) throws Throwable {

    //get chart parameters
    Recordset chartinfo = t.getRecordset("chartinfo");

    //get chart data
    String id = chartinfo.getString("data");
    Recordset data = (Recordset) getSession().getAttribute(id);
    if (data == null)
        throw new Throwable(
                "Invalid Recordset ID:" + id + " - The session does not contain an attribute with this ID.");

    //general chart params
    Integer width = (Integer) chartinfo.getValue("width");
    Integer height = (Integer) chartinfo.getValue("height");

    //load chart plugin
    String plugin = (String) chartinfo.getValue("chart-plugin");
    AbstractChartPlugin obj = (AbstractChartPlugin) Thread.currentThread().getContextClassLoader()
            .loadClass(plugin).newInstance();

    JFreeChart chart = obj.getChart(chartinfo, data);

    //set gradient
    chart.setBackgroundPaint(getGradient());

    //set border and legend params
    chart.setBorderPaint(Color.LIGHT_GRAY);
    chart.setBorderVisible(true);//from   ww w. ja  v  a 2s.  co m
    if (chart.getLegend() != null) {
        chart.getLegend().setBorder(0.2, 0.2, 0.2, 0.2);
        chart.getLegend().setPadding(5, 5, 5, 5);
        chart.getLegend().setMargin(4, 5, 4, 4);
    }

    //render chart in memory
    BufferedImage img = chart.createBufferedImage(width.intValue(), height.intValue());
    ByteArrayOutputStream b = new ByteArrayOutputStream(32768);

    //encode as PNG
    ImageIO.write(img, "png", b);

    //send bitmap via servlet output
    byte image[] = b.toByteArray();
    getResponse().setContentType("image/png");
    getResponse().setContentLength(image.length);
    OutputStream out = getResponse().getOutputStream();
    out.write(image);
    out.close();

    //save image bytes in session attribute if requested
    if (chartinfo.containsField("session")) {
        String session = chartinfo.getString("session");
        if (session != null && session.equals("true"))
            getSession().setAttribute(chartinfo.getString("image-id"), image);
    }

}

From source file:de.qabel.qabelbox.fragments.WelcomeDisclaimerFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof WelcomeScreenActivity) {
        mActivity = (WelcomeScreenActivity) activity;
    } else {/*from   ww w .  ja  va2 s. c om*/
        new Throwable("fragment can't attach to non WelcomeScreenActivity");
    }

}

From source file:id.zelory.benih.util.BenihServiceGenerator.java

public static <S> S createService(Class<S> serviceClass, String baseUrl) {
    RestAdapter.Builder builder = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.BASIC)
            .setConverter(new GsonConverter(Bson.pluck().getParser()))
            .setRequestInterceptor(new RequestInterceptor() {
                @Override//from  ww  w. ja  v  a2  s  . co m
                public void intercept(RequestFacade request) {
                    request.addHeader("Authorization", "Token token=VrlpjCYILseTFuBbRtVN2w");
                }
            }).setErrorHandler(new ErrorHandler() {
                @Override
                public Throwable handleError(RetrofitError cause) {
                    if (cause.getKind().equals(RetrofitError.Kind.HTTP)) {
                        String json = new String(((TypedByteArray) cause.getResponse().getBody()).getBytes());
                        try {
                            JSONObject object = new JSONObject(json);
                            return new Throwable(object.getJSONObject("data").getString("message"));
                        } catch (Exception e) {
                            return cause;
                        }
                    } else if (cause.getKind().equals(RetrofitError.Kind.NETWORK)) {
                        return new Throwable("Can't connect to server, please check your internet connection!");
                    }
                    return cause;
                }
            }).setEndpoint(baseUrl);

    RestAdapter adapter = builder.build();

    return adapter.create(serviceClass);
}

From source file:fi.jumi.core.api.StackTraceTest.java

@Test
public void has_same_stack_trace_as_original_exception() {
    Throwable original = new Throwable("original message");

    StackTrace copy = StackTrace.from(original);

    assertThat(copy.getStackTrace(), is(arrayContaining(original.getStackTrace())));
    assertThat(Throwables.getStackTraceAsString(copy), is(Throwables.getStackTraceAsString(original)));
}

From source file:dinamica.SyncChartOutput.java

public void print(GenericTransaction t) throws Throwable {

    //get chart parameters
    Recordset chartinfo = t.getRecordset("chartinfo");

    //get chart data
    String id = chartinfo.getString("data");
    Recordset data = (Recordset) getSession().getAttribute(id);
    if (data == null)
        throw new Throwable(
                "Invalid Recordset ID:" + id + " - The session does not contain an attribute with this ID.");

    //general chart params
    Integer width = (Integer) chartinfo.getValue("width");
    Integer height = (Integer) chartinfo.getValue("height");

    //load chart plugin
    String plugin = (String) chartinfo.getValue("chart-plugin");
    AbstractChartPlugin obj = (AbstractChartPlugin) Thread.currentThread().getContextClassLoader()
            .loadClass(plugin).newInstance();

    JFreeChart chart = null;/*from w ww .j  av  a 2s.c  o m*/
    synchronized (data) {
        chart = obj.getChart(chartinfo, data);
    }

    //set gradient
    chart.setBackgroundPaint(getGradient());

    //set border and legend params
    chart.setBorderPaint(Color.LIGHT_GRAY);
    chart.setBorderVisible(true);
    if (chart.getLegend() != null) {
        chart.getLegend().setBorder(0.2, 0.2, 0.2, 0.2);
        chart.getLegend().setPadding(5, 5, 5, 5);
        chart.getLegend().setMargin(4, 5, 4, 4);
    }

    //render chart in memory
    BufferedImage img = chart.createBufferedImage(width.intValue(), height.intValue());
    ByteArrayOutputStream b = new ByteArrayOutputStream(32768);

    //encode as PNG
    ImageIO.write(img, "png", b);

    //send bitmap via servlet output
    byte image[] = b.toByteArray();
    getResponse().setContentType("image/png");
    getResponse().setContentLength(image.length);
    OutputStream out = getResponse().getOutputStream();
    out.write(image);
    out.close();

    //save image bytes in session attribute if requested
    if (chartinfo.containsField("session")) {
        String session = chartinfo.getString("session");
        if (session != null && session.equals("true"))
            getSession().setAttribute(chartinfo.getString("image-id"), image);
    }

}