Results 1 to 12 of 12
Like Tree4Likes
  • 1 Post By jim829
  • 3 Post By gimbal2

Thread: Redefining Exception

  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Redefining Exception

    I am trying to redefine the FileNotFoundException by creating a new class which extends it, but I am having difficulty. For my class practice, if no file is selected or passed in, an UnknownFileException should occur.

    My code is the following:

    Java Code:
    import java.io.FileNotFoundException;
    public class UnknownFileException extends FileNotFoundException {
        public UnknownFileException() {
            super("We couldn't tell what file this is");    
        }
        public UnknownFileException(String message) {
            super(message);  
        } 
    }
    Now, my other code which throws this exception:

    Java Code:
    public class practice {
        public String[] array;   
        public String line;
        public PrintWriter outputStream = null;   
        public Scanner inputStream = null; 
       
        public practice(String fileName) {
            array = new String[100];
           
            try {                   
                inputStream = new Scanner(new FileReader(fileName));
                line = inputStream.nextLine();
                array[0] = line;
                for (int i = 1; i < array.length; i++) {
                    array[i] = inputStream.nextLine();   
                }
               inputStream.close();
               outputStream = new PrintWriter(new FileOutputStream("newFile.txt"));
               //outputStream.close();?       
           
           } catch(UnknownFileException e) {
               System.out.println(e.getMessage());
           }  
        }
    But I get a compile error stating an unreported FileNotFoundException.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Redefining Exception

    compile error stating an unreported FileNotFoundException
    Your code uses new FileReader(filename) which could throw a FileNoFoundException. The compiler is saying that you are not catching this exception (or declaring it to be thrown). It is no good catching FileNotFoundException because, as far as the compiler is concerned, the constructor doesn't (or need not) throw that particular sort of FileNotFoundException.

    Notice you are *catching* exceptions in this code, not throwing them. Perhaps you should be thinking about constructing an UnknownFileException and throwing it at some point.

  3. #3
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Redefining Exception

    Quote Originally Posted by pbrockway2 View Post
    Your code uses new FileReader(filename) which could throw a FileNoFoundException. The compiler is saying that you are not catching this exception (or declaring it to be thrown). It is no good catching FileNotFoundException because, as far as the compiler is concerned, the constructor doesn't (or need not) throw that particular sort of FileNotFoundException.

    Notice you are *catching* exceptions in this code, not throwing them. Perhaps you should be thinking about constructing an UnknownFileException and throwing it at some point.
    Hm, I don't think I follow. Do you mean I should use an if statement using throw new FileNotFoundException? When I change my UnknowFileException to FileNotFoundException it works fine.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Redefining Exception

    I believe this falls into the concept of chaining exceptions. You catch the original exception, interrogate the message, and then rethrow a new exception of the desired type.

    Regards,
    Jim
    Robben likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Redefining Exception

    In these kind of situations it helps to just make a stupid comparison to the real world.

    Situation: a guy and a cow stand on a mountain. This guy is about to toss the cow off the mountain. You stand at the bottom, ready to catch that cow.

    You think to yourself: "I can't catch a cow! That is way too heavy, it will kill me. You know what, I'm going to pretend that the cow is a puppy."

    Java Code:
    public class Puppy extends Cow {
    
    }
    The guy at the top of the mountain throws the cow.

    Java Code:
    throw(cow);
    and you attempt to catch your puppy.

    Java Code:
    catch(puppy);
    What do you think will be tumbling down the mountain? A cow or a puppy? I'll give you a hint: that puppy is going to be very heavy.
    DarrylBurke, Norm and Robben like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Redefining Exception

    Quote Originally Posted by gimbal2 View Post
    In these kind of situations it helps to just make a stupid comparison to the real world.

    Situation: a guy and a cow stand on a mountain. This guy is about to toss the cow off the mountain. You stand at the bottom, ready to catch that cow.

    You think to yourself: "I can't catch a cow! That is way too heavy, it will kill me. You know what, I'm going to pretend that the cow is a puppy."

    Java Code:
    public class Puppy extends Cow {
    
    }
    The guy at the top of the mountain throws the cow.

    Java Code:
    throw(cow);
    and you attempt to catch your puppy.

    Java Code:
    catch(puppy);
    What do you think will be tumbling down the mountain? A cow or a puppy? I'll give you a hint: that puppy is going to be very heavy.
    Lol! I have edited my code using the suggestions you guys mentioned (well hopefully I did) but, it doesn't do what I want it to do.

    My code is the following:

    Java Code:
    public class practice {
        public String[] array;  
        public String line;
        public PrintWriter outputStream = null;  
        public Scanner inputStream = null; 
      
        public practice(String fileName)  throws UnknownFileException {
            array = new String[100];
           
            try {                  
                inputStream = new Scanner(new FileReader(fileName));
                line = inputStream.nextLine();
                array[0] = line;
                for (int i = 1; i < array.length; i++) {
                    array[i] = inputStream.nextLine();  
                }
                
                outputStream = new PrintWriter(new FileOutputStream("newFile.txt"));
                     
          
            } catch(UnknownFileException e) {
                System.out.println(e.getMessage());
            } catch (FileNotFoundException e) {
        		throw new UnknownFileException(e.getMessage());
        	} finally {
        		inputStream.close();    		
        	}
        }
    But now when I go and run my driver and put in an invalid filename I get a nullpointerexception instead? Instead of catching printing out the e.getMessage() I caught, it is now giving me a nullpointerexception. I am really confused in what's wrong with my code.
    Last edited by Robben; 03-26-2015 at 04:48 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Redefining Exception

    when I go and run my driver and put in an invalid filename I get a nullpointerexception instead?
    Copy and post the stacktrace that reports the NullPointerException. It has important information like where the NPE occurred.

  8. #8
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Redefining Exception

    Quote Originally Posted by pbrockway2 View Post
    Copy and post the stacktrace that reports the NullPointerException. It has important information like where the NPE occurred.
    Actually, I figured it out. It was because inputStream was still null. I had to change my finally code to if (inputStream != null) and then I can close the inpurStream, i.e.:

    Java Code:
    finally {
        if (inputStream != null) {
            inputStream.close();
        }  
    }

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Redefining Exception

    That might seem like it is cumbersome to have to do, that's why the Java language designers have you covered:

    https://docs.oracle.com/javase/tutor...urceClose.html

    ;) But its a good thing to learn to program defensively in finally clauses.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Redefining Exception

    Quote Originally Posted by gimbal2 View Post
    That might seem like it is cumbersome to have to do, that's why the Java language designers have you covered:

    https://docs.oracle.com/javase/tutor...urceClose.html

    ;) But its a good thing to learn to program defensively in finally clauses.
    Can you elaborate please? Is my code not written well?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Redefining Exception

    Quote Originally Posted by Robben View Post
    Can you elaborate please? Is my code not written well?
    It's not a matter of being well written. Its a matter of simply not using some of the features offered in Java. Personally, I don't like the try-with-resources model. But I am probably in a minority.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Redefining Exception

    Quote Originally Posted by jim829 View Post
    It's not a matter of being well written. Its a matter of simply not using some of the features offered in Java. Personally, I don't like the try-with-resources model. But I am probably in a minority.

    Regards,
    Jim
    Oh i see, thanks for all the help guys!

Similar Threads

  1. Replies: 5
    Last Post: 05-11-2014, 08:42 PM
  2. Difference between overriding and redefining...
    By asif_plz in forum New To Java
    Replies: 6
    Last Post: 08-06-2012, 10:51 AM
  3. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  4. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •