Lecture 5 - File IO Exceptions Flashcards
How do you delegate catches?
You can use the word throws, e.g.
public static void a() throws HighLevelException{ try { b(); } catch (MidLevelException e) { throws new HighLevelException(e); }}
If you don’t want to deal with the exception, you can delegate it up to whatever called your method
Here both exceptions are name of a class extends exception
What does the file reader do?
Where should you initialise it?
It reads individual characters
It should be initalised as a global variable eg.,
FileReader fr = null;
and can be used in the try catch blocks fr = new FileReader();
How do you use String.split method?
String line = “Anna Bergin, 26”
String[] tokens = line.split(“,”);
Note its stored in a string array
What is an example of a try-catch block?
Sometimes the code below will throw an ArrayIndexOutOfBoundsException if pos>=10, but we can include a try, catch, finally block to catch any unchecked exceptions.
Either go into the try/catch, and always go into the finally.
import java.util.Random; public class PracticeFromLectures { public static void main(String[] args) { int[] x = new int[10]; Random r = new Random(); try{ int pos = r.nextInt(20); System.out.println(pos); System.out.println(x[pos]); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Too big!"); } finally { System.out.println("This prints anyway"); } }
What is the hierarchy of java.io.FileNotFoundException? i.e. where does it inherit from?
java. io.FileNotFoundException inherits from:
java. io.IOException, which inherits from:
java. lang.Exception, which inherits from:
java. lang.Throwable, which inherits from:
java. lang.Object
You can catch a FileNotFoundException as Throwable, Exception and IOException. As you go up, it gets more abstract
What are the 2 types of exceptions?
checked & unchecked
How do you print the error message in the catch block?
e.printStackTrace();
How do you convert a String to an int?
Give an example.
Integer.parseInt(element[0])
Returns the integer value which is represented by the argument in decimal equivalent.
or
String[] element = line.split(“ “);
int numOne - Integer.parseInt(element[0]);
How does the fr.read(); work?
It reads all of the chars in a file
It returns -1 if there are no more chars
sometimes it is useful to create an empy array of chars that can be read & printed
eg., char elements = new char [100];
How & when do you close a FileReader?
1. You always close it in the finally block within its own try catch block. Different ways to do it, but one is: try { if(fr!=null) { fr.close(); catch (IOException e) { // don't need to add anything here if it is the end of the fie
If you use the FR at other places you will need to close them so you might end up closing the file reader more than once.
How do you tell a Scanner to keep reading the file if there is another line?
s.hasNextLine();
How do you pass what a scanner has read to another scanner and ask it to read the next int?
Give an example?
Scanner s = new Scanner(System.in); String line = s.hasNextLine(); Scanner t = new Scanner(line); t.nextInt();
What other things can boolean methods produce except returning true/false?
Can work in print methods. Eg.,
boolean false = gameover
while(!gameover) when they get to the bit that is true add gameover = true; end of the while loop could write.. "Well done, you finished the game!"
How do you check if 2 strings are the same?
s.equals();
What are unchecked exceptions?
Red text that may appear often or you can force it by using throw
public class Throw { public static void main(String[] args) { System.out.println("About to throw an unchecked exception"); throw new ArrayIndexOutOfBoundsException(); }}