View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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  package org.supercsv.exception;
17  
18  import org.supercsv.util.CsvContext;
19  
20  /**
21   * Generic SuperCSV Exception class. It contains the CSV context (line number, column number and raw line) from when the
22   * exception occurred.
23   * 
24   * @author Kasper B. Graversen
25   * @author James Bassett
26   */
27  public class SuperCsvException extends RuntimeException {
28  	
29  	private static final long serialVersionUID = 1L;
30  	
31  	private CsvContext csvContext;
32  	
33  	/**
34  	 * Constructs a new <tt>SuperCsvException</tt>.
35  	 * 
36  	 * @param msg
37  	 *            the exception message
38  	 */
39  	public SuperCsvException(final String msg) {
40  		super(msg);
41  	}
42  	
43  	/**
44  	 * Constructs a new <tt>SuperCsvException</tt>.
45  	 * 
46  	 * @param msg
47  	 *            the exception message
48  	 * @param context
49  	 *            the CSV context
50  	 */
51  	public SuperCsvException(final String msg, final CsvContext context) {
52  		super(msg);
53  		if (context != null) {
54  			this.csvContext = new CsvContext(context);
55  		}
56  	}
57  	
58  	/**
59  	 * Constructs a new <tt>SuperCsvException</tt>.
60  	 * 
61  	 * @param msg
62  	 *            the exception message
63  	 * @param context
64  	 *            the CSV context
65  	 * @param t
66  	 *            the nested exception
67  	 */
68  	public SuperCsvException(final String msg, final CsvContext context, final Throwable t) {
69  		super(msg, t);
70  		if (context != null) {
71  			this.csvContext = new CsvContext(context);
72  		}
73  	}
74  	
75  	/**
76  	 * Gets the current CSV context.
77  	 * 
78  	 * @return the current CSV context, or <tt>null</tt> if none is available
79  	 */
80  	public CsvContext getCsvContext() {
81  		return csvContext;
82  	}
83  	
84  	/**
85  	 * Returns the String representation of this exception.
86  	 */
87  	@Override
88  	public String toString() {
89  		return String.format("%s: %s%ncontext=%s", getClass().getName(), getMessage(), csvContext);
90  	}
91  }