View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // UTGB Common Project
18  //
19  // UTGBException.java
20  // Since: 2007/03/27
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome;
26  
27  import java.io.IOException;
28  
29  /**
30   * A UTGBException is a base class for all exception classes in the org.utgb packages
31   * 
32   * @author leo
33   * 
34   */
35  @SuppressWarnings("serial")
36  public class UTGBException extends Exception {
37  	private UTGBErrorCode errorCode = UTGBErrorCode.Unknown;
38  
39  	public UTGBException(String message) {
40  		super(message);
41  	}
42  
43  	public UTGBException(Throwable cause) {
44  		super(cause);
45  	}
46  
47  	public UTGBException(String message, Throwable cause) {
48  		super(message, cause);
49  	}
50  
51  	public UTGBException(UTGBErrorCode errorCode) {
52  		super();
53  		this.errorCode = errorCode;
54  	}
55  
56  	public UTGBException(UTGBErrorCode errorCode, Throwable cause) {
57  		super(cause);
58  		this.errorCode = errorCode;
59  	}
60  
61  	public UTGBException(UTGBErrorCode errorCode, String message, Throwable cause) {
62  		super(message, cause);
63  		this.errorCode = errorCode;
64  	}
65  
66  	public UTGBException(UTGBErrorCode errorCode, String message) {
67  		super(message);
68  		this.errorCode = errorCode;
69  	}
70  
71  	public UTGBErrorCode getErrorCode() {
72  		return this.errorCode;
73  	}
74  
75  	@Override
76  	public String getMessage() {
77  		return "[" + errorCode.name() + "] " + super.getMessage();
78  	}
79  
80  	public static UTGBException convert(Exception e) {
81  		if (UTGBException.class.isInstance(e)) {
82  			UTGBException xe = UTGBException.class.cast(e);
83  			return xe;
84  		}
85  		else if (IOException.class.isInstance(e)) {
86  			return new UTGBException(UTGBErrorCode.IO_ERROR, e);
87  		}
88  		else {
89  			return new UTGBException(UTGBErrorCode.INHERITED, e);
90  		}
91  	}
92  
93  }