1 /*
2 * Copyright 2008, 2009 Ange Optimization ApS
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 eu.simuline.octave.exception;
17
18 import eu.simuline.octave.type.OctaveObject;
19
20 /**
21 * Exception thrown when a cast fails inside JavaOctave.
22 */
23 public final class OctaveClassCastException extends OctaveRecoverableException {
24
25 private final OctaveObject octaveObject;
26
27 private final Class<? extends OctaveObject> castClass;
28
29 /**
30 * @param cause
31 * ClassCastException that we are rethrowing
32 * @param octaveObject
33 * OctaveObject that couldn't be casted
34 * @param castClass
35 * The class octaveObject couldn't be casted to
36 */
37 public OctaveClassCastException(ClassCastException cause,
38 OctaveObject octaveObject,
39 Class<? extends OctaveObject> castClass) {
40 super(cause);
41 this.octaveObject = octaveObject;
42 this.castClass = castClass;
43 }
44
45 /**
46 * @return the OctaveObject that couldn't be casted
47 */
48 public OctaveObject getOctaveObject() {
49 return octaveObject;
50 }
51
52 /**
53 * @return the class octaveObject couldn't be casted to
54 */
55 public Class<? extends OctaveObject> getCastClass() {
56 return castClass;
57 }
58
59 @Override
60 public String getMessage() {
61 final String m1 = "Failed cast of " + octaveObject + " to " + castClass;
62 final String m2 = super.getMessage();
63 return m1 + (m2 == null ? "" : ", " + m2);
64 }
65
66 }