Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Please help me!!!! package debugmeone; import java.io.File; import java.io.Fil

ID: 3853665 • Letter: #

Question

//Please help me!!!!

package debugmeone;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/*
* The output should be:
*
*   run:
*   Error - File Not Found: accountrecords.txt
*   BUILD SUCCESSFUL (total time: 1 seconds)
*/
public class ReadTextFile {

    private Scanner input; // Ignore the hint given by NetBeans

    public void openFile() {
        try {
            input = new Scanner(new File("accountrecords.txt"));
        } catch (Exception e) {
            System.out.println("Something bad just happened here.");
        }
        catch (FileNotFoundException fnfe) {
            System.out.println("Error - File Not Found: accountrecords.txt");
        }
    }
}

---------

package debugmeone;

/*
* Nothing to debug in this file.
*/
public class ReadTextFileTest {
    public static void main(String[] args)
    {
        ReadTextFile data = new ReadTextFile();
        data.openFile();
    }
}


---------------------------------------------------------------------------------------------------------------------------------------------

package debugmetwo;
/*
* This is a custom exception. All custom exceptions that you would create
* must extend (i.e. inherit) from class Exception.
*
* There is no need to debug this file.
*/
public class EagleLandingException extends Exception
{
    public EagleLandingException(String msg)
    {
        super(msg);
    }
}

---------

package debugmetwo;
/*
* You will need to debug this file.
*
* The output should be:
*
* run:
* There is a problem with the Eagle!
* Java Result: 9999
* BUILD SUCCESSFUL (total time: 0 seconds)
*/
public class ThrowEagleExceptionTest {
    public static void main(String[] args)
    {
        try {
            EagleLanding();
        }
        catch(EagleLandingException badEagle)
        {
            System.out.printf("%s ", badEagle.getMessage());
            System.exit(9999);
        }
    }

    private static void EagleLanding()
    {
        EagleLandingException("There is a problem with the Eagle!");
    }
}

Explanation / Answer

in first one.. the issue is order of catch statements... you must place catch of FileNotFoundException before catch of Exception..

right now it is always going to Exception as Exception is a parent class of all exceptions. so order mist be changed like this :-

public class ReadTextFile {

    private Scanner input; // Ignore the hint given by NetBeans

    public void openFile() {
        try {
            input = new Scanner(new File("accountrecords.txt"));
        }

catch (FileNotFoundException fnfe) {
            System.out.println("Error - File Not Found: accountrecords.txt");
        }

catch (Exception e) {
            System.out.println("Something bad just happened here.");
        }
  
    }
}

In second one eagleLanding method must call the object of exception class not the class itself. Like below

private static void EagleLanding() throws EagleLandingException
    {
throw new EagleLandingException("There is a problem with the Eagle!");
    }

let me know if you need anything else in this. I'll answer in comments