Results 1 to 7 of 7

Thread: instanceof ?

Threaded View

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

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