comparison graal/com.oracle.truffle.ruby.runtime/src/com/oracle/truffle/ruby/runtime/control/ExceptionTranslator.java @ 13514:0fbee3eb71f0

Ruby: import project.
author Chris Seaton <chris.seaton@oracle.com>
date Mon, 06 Jan 2014 17:12:09 +0000
parents
children
comparison
equal deleted inserted replaced
13513:64a23ce736a0 13514:0fbee3eb71f0
1 /*
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
4 * redistribute it and/or modify it under the terms of the:
5 *
6 * Eclipse Public License version 1.0
7 * GNU General Public License version 2
8 * GNU Lesser General Public License version 2.1
9 */
10 package com.oracle.truffle.ruby.runtime.control;
11
12 import com.oracle.truffle.api.*;
13 import com.oracle.truffle.ruby.runtime.*;
14 import com.oracle.truffle.ruby.runtime.core.*;
15 import com.oracle.truffle.ruby.runtime.objects.*;
16
17 public final class ExceptionTranslator {
18
19 /**
20 * Translate a Java exception into a Ruby exception.
21 */
22 public static RubyBasicObject translateException(RubyContext context, Throwable exception) {
23 assert context != null;
24 assert exception != null;
25
26 CompilerAsserts.neverPartOfCompilation();
27
28 // RaiseException already includes the Ruby exception
29
30 if (exception instanceof RaiseException) {
31 return ((RaiseException) exception).getRubyException();
32 }
33
34 // Translate divide by zero into ZeroDivisionError
35
36 if (exception instanceof ArithmeticException && (exception.getMessage().endsWith("divide by zero") || exception.getMessage().endsWith("/ by zero"))) {
37 return new RubyException(context.getCoreLibrary().getZeroDivisionErrorClass(), "divided by 0");
38 }
39
40 /*
41 * If we can't translate the exception into a Ruby exception, then the error is ours and we
42 * report it as as RubyTruffleError. If a programmer sees this then it's a bug in our
43 * implementation.
44 */
45
46 if (context.getConfiguration().getPrintJavaExceptions()) {
47 exception.printStackTrace();
48 }
49
50 String message;
51
52 if (exception.getMessage() == null) {
53 message = exception.getClass().getSimpleName();
54 } else {
55 message = exception.getMessage();
56 }
57
58 return new RubyException(context.getCoreLibrary().getRubyTruffleErrorClass(), message);
59 }
60
61 }