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

1. Find the error in each of the following code segments: catch (FileNotFoundExc

ID: 3534904 • Letter: 1

Question

1. Find the error in each of the following code segments:

catch (FileNotFoundException e)

{

System.out.println("File not found.");

}

try

{

File file = new File("MyFile.txt");

Scanner inputFile = new Scanner(file);

}

Explain the error(s) in words, not the modified code.



Find the error in each of the following code segments:

try {

number= Integer.parseInt(str);

}

catch (Exception e) {

System.out.println(e.getMessage());

}

catch (IllegalArgumentException e) {

System.out.println("Bad number format.");

}

catch (NumberFormatException e) {

System.out.println(str + "is not a number.");

}

Explain the error(s) in words, not the modified code.



Look at the following program and tell what the program will output when run:

public class ExceptionTest {

public static void main(String[] args) {

int number;

String str;

try {

str = "xyz";

number = Integer.parseInt(str);

System.out.println("A");

}

catch(NumberFormatException e) {

System.out.println("B");

}

catch(IllegalArgumentException e) {

System.out.println("C");

}

System.out.println("D");

}

}


Look at the following program and tell what the program will output when run:

public class ExceptionTest {

public static void main(String[] args) {

int number;

String str;

try {

str = "xyz";

number = Integer.parseInt(str);

System.out.println("A");

}

catch(NumberFormatException e) {

System.out.println("B");

}

catch(IllegalArgumentException e) {

System.out.println("C");

}

finally {

System.out.println("D");

}

System.out.println("E");

}

}



Look at the following program and tell what the program will output when run:

public class ExceptionTest {

public static void main(String[] args) {

int number;

String str;

try {

str = "xyz";

number = Integer.parseInt(str);

System.out.println("A");

}

catch(NumberFormatException e) {

System.out.println("B");

}

catch(IllegalArgumentException e) {

System.out.println("C");

}

finally {

System.out.println("D");

}

System.out.println("E");

}

}



Explanation / Answer

1> we can't catch before try

try code should be executed first than in case of exception it should execute the catch code


2>

these catch code:

catch (IllegalArgumentException e) {

System.out.println("Bad number format.");

}

catch (NumberFormatException e) {

System.out.println(str + "is not a number.");

}

has already been checked in catch code of Exception as it check entire exception in the try block

hence produce error for rechecking it

3>

yes it will run as there is only NumberFormatException exception so it catch it in


catch(NumberFormatException e) {

System.out.println("B");

}

and not ececute IllegalArgumentException checking

catch(IllegalArgumentException e) {
System.out.println("C");

out put"

B

D



}

4>

yes:

B

D

D

4>

yes:

B

D

D