Results 1 to 7 of 7

Thread: instanceof ?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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!!!");
         } 
       }

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    115
    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

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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"); 
        }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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());   
        }
      }
     }
    }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width