Lecture 5 - File IO Exceptions Flashcards

1
Q

How do you delegate catches?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the file reader do?

Where should you initialise it?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you use String.split method?

A

String line = “Anna Bergin, 26”
String[] tokens = line.split(“,”);

Note its stored in a string array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an example of a try-catch block?

A

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");
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the hierarchy of java.io.FileNotFoundException? i.e. where does it inherit from?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 2 types of exceptions?

A

checked & unchecked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you print the error message in the catch block?

A

e.printStackTrace();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you convert a String to an int?

Give an example.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the fr.read(); work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How & when do you close a FileReader?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you tell a Scanner to keep reading the file if there is another line?

A

s.hasNextLine();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you pass what a scanner has read to another scanner and ask it to read the next int?
Give an example?

A
Scanner s = new Scanner(System.in);
String line = s.hasNextLine();
Scanner t = new Scanner(line);
t.nextInt();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What other things can boolean methods produce except returning true/false?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you check if 2 strings are the same?

A

s.equals();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are unchecked exceptions?

A

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();
}}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you make your own exception?

A

Create a class that extends any exception you want

eg.,
public class MyException extends Exception {
17
Q

What exception does a FileReader throw?

A

file reader constructor throws a FileNotFoundException
fr.close() throws an IOException
so we need to do it in a try-catch block within the finally block.

Since IOException is above sometimes if you only need one it will catch both the FileReader constructor and fr.close()

18
Q

What do you do if you don’t want to deal with an exception?

A

Delegate it to the method that called it by using throws in the method declaration:

public void myMethod(String a) throws IOException, ArrayIndexOutOfBoundsException {

19
Q

What do you do if a method doesn’t want to deal with an exception?

A

MyException can be thrown in this method. It doesn’t want to deal with it, so throws it up to the calling method (main).

	public static void main(String[] args) {
		try {
			myMethod();
		}catch(MyException e) {
			e.printStackTrace();
		}
	}
	public static void myMethod() throws MyException {
		// some code
		if(new Random().nextInt(10)<=7) {
			throw new MyException();
		}
	}
20
Q

Can multiple statements catch the same exception?

A

Yes because of polymorphism.

public static void main(String[] args) {
  try {
      throw new FileNotFoundException();
   } catch (IOException e) {
     System.out.println("IO");
} catch(FileNotFoundException e) { //don't need this because exits as soon as FnF is caught
//	System.out.println("FnF");
}

In this case, Java will not compile because all FnF exceptions will be caught by the IOException making the line that prints FnF unreachable

Exceptions are caught by the first (top to bottom) catch statement that they can be caught by

21
Q

How do you keep looping when there are more lines to be read?

A

while(s.hasNextLine() {

String line = s.nextLine();

22
Q

What is BufferWriter?

A

Used for temporary storage

23
Q

What is JFileChooser?

A

Lets a user select a file/folder:

JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(new JFrame());
fc.getSelectedFile();

This will pop up a JFileChooser dialog box returnVal that tells you if a file was chosen or not (0 = yes). And then you can access the file object using a getter.

24
Q

Do we always go into the finally block?

A

Yes we either go into the try/catch, and always go into the finally.