|
-
Apr 21st, 2003, 12:23 AM
#1
Thread Starter
Dazed Member
instanceof ?
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. 
Code:
// no casting necessary
try{
.........
}catch(Exception e){
if(e instanceof FileNotFoundException){
FileNotFoundException fnf = (FileNotFoundException) e;
fnf.getMessage();
}
}
Code:
// 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!!!");
}
}
Last edited by Dilenger4; May 8th, 2003 at 12:08 AM.
-
Apr 22nd, 2003, 02:04 AM
#2
Lively Member
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
-
Apr 22nd, 2003, 02:21 AM
#3
Thread Starter
Dazed Member
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.
Code:
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");
}
Last edited by Dilenger4; Apr 22nd, 2003 at 03:36 AM.
-
Apr 22nd, 2003, 03:43 AM
#4
Thread Starter
Dazed Member
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.
-
Apr 22nd, 2003, 03:46 AM
#5
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 2nd, 2003, 01:32 AM
#6
Thread Starter
Dazed Member
Yup works.
Code:
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());
}
}
}
}
-
May 2nd, 2003, 05:23 AM
#7
Code:
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
Code:
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|