Question

In: Computer Science

Exercise 2: Try-Catch Exercise Write a Java code that does the following: Create a class MyClass...

Exercise 2:

Try-Catch Exercise

Write a Java code that does the following:

Create a class MyClass and create three methods myMethod1(), Method2() and Method3().

Invoke Method2() from Method1() and Method3() from Method2().

Write a code that can throw an exception inside myMethod3() and compile:

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

Scanner sc=new Scanner(file);

You will get compilation errors now, as you are not handling a checked exception FileNotFoundException.

Declare throws over myMethod3() and myMethod2(). You will need to add throws FileNotFoundException on myMethod() as:

public void myMethod3() throws FileNotFoundException

{

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

Scanner sc=new Scanner(file);

}

Handle the exception in myMethod1() by enclosing the code that can throw exception using a try-catch block:

public void myMethod1()

{

try{

myMethod2();

}

catch(FileNotFoundException e)

{

e.printStackTrace();

}

}

Solutions

Expert Solution

Program

import java.util.*;
import java.io.*;
public class MyClass {
public void myMethod1(){
   try {
       myMethod2();
   } catch(FileNotFoundException e) {
       e.printStackTrace();
   }
}
public void myMethod2()throws FileNotFoundException {
   myMethod3();
}
public void myMethod3() throws FileNotFoundException {
   File file=new File("filename.txt");
   Scanner sc=new Scanner(file);
}
public static void main(String args[]){
   MyClass m=new MyClass();
   m.myMethod1();
}
}


Related Solutions

In-Class coding exercise: (try - catch) (pass / fail) The purpose of this in-class coding exercise...
In-Class coding exercise: (try - catch) (pass / fail) The purpose of this in-class coding exercise is to simulate various exception conditions using the program provided. package com.InClass; /* * This program demonstrates the ability of catching different types of exceptions" * You will have to modify the method() codes to trigger various/different exceptions. * inClass Coding ( you will have to remove (comment out) the previous error condition before moving to trigger the next) * 1) throw a myExcept...
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
What is Try and Catch in Java with example?
What is Try and Catch in Java with example?
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Exercise 1: You are required to re-write the following code to catch the exception that can...
Exercise 1: You are required to re-write the following code to catch the exception that can occur. import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); // Display the result System.out.println( "The number entered is " + number);           } }
Write Java code for each of the following problem a. Two players, a and b, try...
Write Java code for each of the following problem a. Two players, a and b, try to guess the cost of an item. Whoever gets closest to the price without going over is the winner. Return the value of the best bid. If both players guessed too high, return -1. example: closestGuess(97, 91, 100) → 97 closestGuess(3, 51, 50) → 3 closestGuess(12, 11, 10) → -1 b. Given a non-empty string, return true if at least half of the characters...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT