Question

Java: In the code example below, explain the purpose of the catch block. (The last 4...

Java:

In the code example below, explain the purpose of the catch block. (The last 4 lines of code.)

FileInputStream inFileStream;

Scanner         scanner;

scanner = new Scanner( System.in );

System.out.print("Enter filename to open: ");

String filename = scanner.next();

try {

     InFileStream = new FileInputStream( filename );

} catch (Exception e)
{

     inFileStream = new FileInputStream("default.dat");

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here we are trying open a file which is given by user so if it not exist in the system than it will open the default file in the catch block

So when we are trying open user given file if the file does not exist than it throws exception and controls comes to the catch block in the catch we will try to open the default file as user given file does not exist

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Java: In the code example below, explain the purpose of the catch block. (The last 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting...

    How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting until the user enters a valid number. I know that I would need to try n = kbd.nextInt(); and catch a InputMismatchException and if n<1 or n>100 then I need to print Out of Range Exception. Must be in 1..100. However, I am unsure how to loop all of that. import java.io.*; import java.util.*; public class Lab5 { public static void main( String args[]...

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • in JAVA, why my code can run, but it does not show any files on my...

    in JAVA, why my code can run, but it does not show any files on my desktop. Scanner k = new Scanner(System.in); System.out.println(" Please enter the first filename: "); String fileName1 = k.nextLine(); System.out.println(" Please enter the second filename: "); String fileName2 = k.nextLine(); File f1 = new File(fileName1); Scanner file1 = new Scanner(f1); PrintWriter f2 = new PrintWriter(fileName2);    while(file1.hasNext()){ String str= fileName1.toUpperCase(); f2.println(str.toUpperCase()); } file1.close(); f2.close(); }

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • Hello, I am continuously receiving errors in the code below. Can you please show me what...

    Hello, I am continuously receiving errors in the code below. Can you please show me what I am doing wrong? import java.util.*; public class ShowTax { { public static void main(String [] args) {        Tax myTax = new Tax();        String ssn, maritalStatus, lname, fname,zipcode;        double income;        int numberOfVehicles;        Scanner s = new Scanner(System.in);        String regex = "[0-9]{3}-[0-9]{2}-[0-9]{4}";        do {            System.out.print("Enter the Social Security...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT