I am trying to throw my custom exception in java but currently I have had no luck. I have two classes a readWrite class which allows the user to enter the file name and a text to be wrote to the file (via the constructor). It has three methods, write, read and a writeToFile which validates if the file ends in .txt if it does now it should throw my custom exception class stating that "Sorry but this system only accepts .txt files" which I have created in a toString() method in my custom exception. I can not seem to make it work, below is the code, some might be able to help, I hope I have explained properly as I am new to Java, note I have commented out some code as I was trying a few different things to make it work

**ReadWrite.java**

```
import java.io.*;

public class ReadWrite
{   
    private final String file;
    private final String text;

    public ReadWrite(String file, String text)
    {
        // initialise instance variables
        this.file=file;
        this.text=text;   
    }

    private void write() //throws InvalidFileException 
    {
        try {
            FileWriter writer = new FileWriter(file);

            writer.write(text);
            writer.write('\n');
            writer.close();
        }
        catch(IOException e)
        {
            System.out.print(e);
        }
    }

    public boolean writeToFile() 
    {    
        boolean ok;

        try{        
            FileWriter writer = new FileWriter(file);       
            {   
                if(file.toLowerCase().endsWith(".txt"))
                {  
                    write();
                    ok = true;
                } //if end
                else{
                    ok=false;
                    //throw new InvalidFileException();
               } //else end
            }
        } //try end
        catch(IOException e) {
           ok=false; 
        } // catch end         
        //catch (InvalidFileException e){

            //System.out.println(e.toString());
        //}
        return ok;
    }

    public void read(String fileToRead)
    {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileToRead));
            String line = reader.readLine();

            while(line != null) {
               System.out.println(line);
               line = reader.readLine();
            }// while end

            reader.close();

        }//try end
        catch(FileNotFoundException e) {                  
            System.out.println(fileToRead + " the system can not     find the file specified");                  
        }  //catch end
        catch(IOException e) {                          
            e.printStackTrace();

        } //catch end
    }
}

```

**InvalidFileException.java**

```
import java.io.FileNotFoundException;
import java.io.*;

public class InvalidFileException extends Exception
{
    /**
     * Constructor for objects of class InvalidFileException
     */
    public InvalidFileException(String message)
    {
        super(message);
    }

    public String toString()
    {
        return ("Sorry but this system only accepts .txt files");
    }
}

```