What is the difference between throwing an exception and catching an exception?
ID: 3569192 • Letter: W
Question
- What is the difference between throwing an exception and catching an exception? Provide an answer in two to three sentences.
- Implement the method getSequence that takes a Scanner as a parameter. It should read Strings one line at a time from the Scanner and store them in an ArrayList<String> until it reaches a line with a single '.' character on it. It should then return the array list to the calling program.
public static ArrayList<String> getSequence(Scanner in)
- Now implement the method summarizeSequences that takes a String as a parameter. This method should open the file named by this String, and read a sequence from that file into an array list using the getSequence method you wrote above. It should then display on a single line the first, last, and middle String in the sequence (if the list is even, the position of the "middle" String is the size of the list divided by 2). Make sure you catch and handle the possible IOException exceptions.
public static void summarizeSequences(String fileName)
- Now implement the method writeSequenceReverse that takes a PrintWriter and an ArrayList<String> as parameters. This method shouldwrite the Strings in the ArrayList to the PrintWriter in reverse order and then write a line with a single '.' on it.
public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list)
- Now implement a method reverseSequences that takes two Strings as parameters. The first String is the name of an input file containing String values and the second is the name of an output file to write the reverse sequences to. The method should read sequences from the input file one sequence at a time using getSequence and then write them out to the output file. Do not forget to close the output file when the method is finished. Make sure you catch and handle the possible IOException exceptions.
public static void reverseSequences(String inputFile, String outputFile)
Explanation / Answer
Q1)
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
Creating an exception object and handing it to the runtime system is called throwing an exception.
Handiling that thrown Exception in our own way is called as catching exception
Q2)
public static ArrayList<String> getSequence(Scanner in)
{
ArrayList<String> al=new ArrayList<String>();
String s="";
while(!(s=in.nextLine()).equals("."))
{
al.add(s);
}
return al;
}
Q3)
public static void summarizeSequences(String fileName)
{
try
{
Scanner sc=new Scanner(new FileReader("fileName"));
ArrayList<String> al=getSequence(sc);
System.out.println(al.get(0)+" "+al.get(al.size()/2)+" "+al.get(al.size()-1));
}catch(IOException e){System.out.println(e.getMessage());}
}
Q4)
public static void writeSequenceReverse(PrintWriter out, ArrayList<String> list)
{
for(int i=list.size()-1;i>=0;i--)
{
out.write(list.get(i)+" ");
}
out.write(".");
out.close();
}
Q5)
public static void reverseSequences(String inputFile, String outputFile)
{
try{
Scanner sc=new Scanner(new FileReader(inputFile));
FileWriter fw=new FileWriter(inputFile);
String in="";
while(!(in=sc.nextLine()).equals("."))
{
fw.write(in+" ");
}
fw.close();
}catch(IOException e){System.out.println(e.getMessage());}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.