Click to See Complete Forum and Search --> : instanceof ?
Dillinger4
Apr 21st, 2003, 01:23 AM
Im trying to use the instanceof operator within the context of error handling but the code seems to be not working properly.
I just want to specify one superclass and be able to determine which subclass is being caught. No casting is necessary since i dont need to work with the object.
Thanks for any help. :)
// no casting necessary
try{
.........
}catch(Exception e){
if(e instanceof FileNotFoundException){
FileNotFoundException fnf = (FileNotFoundException) e;
fnf.getMessage();
}
}
// what i would like to do
try{
............
}catch(Exception e){
if(e instanceof FileNotFoundException){
System.out.println("FileNotFoundException!!!");
}
if(e instanceof NullPointerException){
System.out.println("NullPointerException!!!");
}
}
CreoN
Apr 22nd, 2003, 03:04 AM
Do you get any errors?
I've never used the instanceOf thingie so I'm not sure I can help you with that, but try using:
e.getClass().getSuperClass().getName()
or something like that. Not sure about the methods, but that you can look up in the API :)
Dillinger4
Apr 22nd, 2003, 03:21 AM
Actually i figured out that another catch block was catching the Exception being thrown. So what i was trying to do might work after all. In the first if statement if the mode !null(and matches) but the file == null the compress file method is still invoked with a null string being passed to the method. I need it where if either one is null then the if statement is executed.
if((mode == null) || (file == null)){
System.out.println("enter mode file");
break;
}
if(mode.equals("-c")){
compress(file);
}
else if(mode.equals("-d")){
decompress(file);
}
else{
System.out.println("Correct mode needed");
}
Dillinger4
Apr 22nd, 2003, 04:43 AM
Fixed it. Changed the line to if(mode.equals("") || file.equals("")). I assumed that readLine() returned a null value when the enter key was pressed. :rolleyes:
CornedBee
Apr 22nd, 2003, 04:46 AM
The instanceof thing would work, but
a) getMessage is AFAIK a method of Exception too, so you can simply call it on e without any casting.
b) Have several catch blocks with diffferent classes. It's more efficient.
Dillinger4
May 2nd, 2003, 02:32 AM
Yup works. :p
public class ThrowTest{
public static void main(String[] args){
try {
int i = 7 / 0;
}catch(Exception e){
if(e instanceof ArithmeticException){
System.out.println("ArithmeticException");
System.out.println(e.getMessage());
}
if(e instanceof ClassCastException){
System.out.println("ClassCastException");
System.out.println(e.getMessage());
}
if(e instanceof RuntimeException){
System.out.println("RuntimeException");
System.out.println(e.getMessage());
}
if(e instanceof Exception){
System.out.println("Exception");
System.out.println(e.getMessage());
}
if(e instanceof Throwable){
System.out.println("Throwable");
System.out.println(e.getMessage());
}
}
}
}
CornedBee
May 2nd, 2003, 06:23 AM
public class ThrowTest{
public static void main(String[] args){
try {
int i = 7 / 0;
}catch(Exception e){
System.out.println(e.getClass().getName());
System.out.println(e.getMessage());
}
}
}
or
public class ThrowTest{
public static void main(String[] args){
try {
int i = 7 / 0;
} catch(ArithmeticException e) {
System.out.println("ArithmeticException");
System.out.println(e.getMessage());
} catch(ClassCastException e) {
System.out.println("ClassCastException");
} // ...
}
}
The main difference to yours is that mine doesn't run the hierarchy. In your example your code would print:
ArithmeticException
Integer division by zero
RuntimeException
Integer division by zero
Exception
Integer division by zero
Throwable
Integer division by zero
while mine would only print
ArithmeticException (or java.lang.ArithmeticException)
Integer division by zero
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.