1 module java.lang.exceptions;
2 
3 import java.lang.util;
4 import java.lang.String;
5 
6 version(Tango){
7     static import tango.core.Exception;
8     public alias tango.core.Exception.IllegalArgumentException IllegalArgumentException;
9     public alias tango.core.Exception.IOException IOException;
10     public alias tango.core.Exception.PlatformException PlatformException;
11     public alias tango.core.Exception.ArrayBoundsException ArrayIndexOutOfBoundsException;
12     public alias tango.core.Exception.NoSuchElementException NoSuchElementException;
13     public alias tango.core.Exception.UnicodeException UnicodeException;
14     alias Exception Throwable;
15 } else { // Phobos
16 
17     static import core.exception;
18     public alias core.exception.RangeError ArrayIndexOutOfBoundsException;
19 
20     class PlatformException : Exception {
21         this( String e = null ){
22             super(e);
23         }
24     }
25 
26     class IllegalArgumentException : Exception {
27         this( String e = null ){
28             super(e);
29         }
30     }
31 
32     class IOException : Exception {
33         this( String e = null ){
34             super(e);
35         }
36     }
37 
38     class NoSuchElementException : Exception {
39         this( String e = null ){
40             super(e);
41         }
42     }
43 
44     class UnicodeException : Exception {
45         this( String msg, int idx){
46             super( "" );
47         }
48     }
49 
50 }
51 
52 class InternalError : Exception {
53     this( String file, long line, String e = null ){
54         super(e);
55     }
56 }
57 
58 class ArithmeticException : Exception {
59     this( String e = null ){
60         super(e);
61     }
62 }
63 
64 class ClassCastException : Exception {
65     this( String e = null ){
66         super(e);
67     }
68 }
69 
70 class IllegalStateException : Exception {
71     this( String e = null ){
72         super(e);
73     }
74     this( Exception e ){
75         super(e.toString);
76     }
77 }
78 
79 class NoSuchMethodException : Exception {
80     this( String e = null){
81         super(e);
82     }
83 }
84 
85 class IllegalAccessException : Exception {
86     this( String e = null){
87         super(e);
88     }
89 }
90 
91 class SecurityException : Exception {
92     this( String e = null){
93         super(e);
94     }
95 }
96 
97 class IndexOutOfBoundsException : Exception {
98     this( String e = null){
99         super(e);
100     }
101 }
102 
103 class StringIndexOutOfBoundsException : IndexOutOfBoundsException {
104     this( String e = null){
105         super(e);
106     }
107 }
108 
109 class InterruptedException : Exception {
110     this( String e = null ){
111         super(e);
112     }
113     this( Exception e ){
114         super(e.toString);
115     }
116 }
117 
118 class NullPointerException : Exception {
119     this( String e = null ){
120         super(e);
121     }
122     this( Exception e ){
123         super(e.toString);
124     }
125 }
126 
127 class NumberFormatException : IllegalArgumentException {
128     this( String e ){
129         super(e);
130     }
131     this( Exception e ){
132         super(e.toString);
133     }
134     public String getMessage(){
135         return msg;
136     }
137 }
138 
139 class RuntimeException : Exception {
140     this( String file, long line, String msg = null){
141         super( msg, file, cast(size_t) line );
142     }
143     this( String e = null){
144         super(e);
145     }
146     this( Exception e ){
147         super(e.toString);
148         next = e;
149     }
150     public String getMessage(){
151         return msg;
152     }
153     public Throwable getCause() {
154         return next; // D2 has next of type Throwable
155     }
156 }
157 
158 class UnsupportedOperationException : RuntimeException {
159     this( String e = null){
160         super(e);
161     }
162     this( Exception e ){
163         super(e.toString);
164     }
165 }
166 
167 /// Extension to the D Exception
168 String ExceptionGetLocalizedMessage( Exception e ){
169     return e.msg;
170 }
171 
172 version(Tango){
173     /// Extension to the D Exception
174     void ExceptionPrintStackTrace( Exception e ){
175         ExceptionPrintStackTrace( e, & getDwtLogger().error );
176     }
177 
178     /// Extension to the D Exception
179     void ExceptionPrintStackTrace( Throwable e, void delegate ( String file, ulong line, String fmt, ... ) dg ){
180         Throwable exception = e;
181         while( exception !is null ){
182             dg( exception.file, exception.line, "Exception in {}({}): {}", exception.file, exception.line, exception.msg );
183             if( exception.info !is null ){
184                 foreach( frame; exception.info ){
185                     dg( exception.file, exception.line, "trc {} {}", frame.file, frame.line );
186                 }
187             }
188             exception = exception.next;
189         }
190     }
191 } else { // Phobos
192     void ExceptionPrintStackTrace( Exception e ){
193         Throwable exception = e;
194         while( exception !is null ){
195             getDwtLogger().error( exception.file, exception.line, "Exception in {}({}): {}", exception.file, exception.line, exception.msg );
196             if( exception.info !is null ){
197                 foreach( line, file; exception.info ){
198                     getDwtLogger().error( exception.file, exception.line, "trc {} {}", file, line );
199                 }
200             }
201             exception = exception.next;
202         }
203     }
204 }
205 
206 void PrintStackTrace( int deepth = 100, String prefix = "trc" ){
207     version(Tango){
208         auto e = new Exception( null );
209         int idx = 0;
210         const start = 3;
211         foreach( frame; e.info ){
212             if( idx >= start && idx < start+deepth ) {
213                 getDwtLogger().trace( __FILE__, __LINE__, "{} {}: {}", prefix, frame.file, frame.line );
214             }
215             idx++;
216         }
217     } else { // Phobos
218         auto e = new Exception( null );
219         int idx = 0;
220         const start = 3;
221         foreach( line, file; e.info ){
222             if( idx >= start && idx < start+deepth ) {
223                 getDwtLogger().trace( __FILE__, __LINE__, "{} {}: {}", prefix, file, line );
224             }
225             idx++;
226         }
227     }
228 }