Week 5 Flashcards

1
Q

What are the two types of exceptions?

A

Check and unchecked

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

What are checked exceptions?

A

Exceptions that have to be caught

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

What are unchecked exceptions?

A

Exceptions that don’t have to be caught e.g. ArrayIndexOutOfBoundsException

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

How do you make your own exception?

A
You use extend to extend an existing exception, e.g. 
public class MyException extends RuntimeException{ }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you catch exceptions?

A

use try catch and finally blocks

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

What’s an example of a try, catch, finally block?

A
import java.util.Random;
public class PracticeFromLectures {
	public static void main(String[] args) {
		int[] x= new int[10]; //int array with length 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
7
Q

What’s an example of a checked exception?

A

FileNotFoundException - if the named file doesn’t exist or it’s a directory (or not a file) it’ll throw an exception

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

How do we close FileReader objects?

A

in the finally block, we can have try and catch. In the try statement, we can close the FileReader.

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

How do you delegate catches?

A

You can use the word throws, e.g.
public void myMethod(String a) throws IOException, ArrayIndexOutOfBoundsException { }

If you don’t want to deal with the exception, you can delegate it up to whatever called your method

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

What does the FileReader do?

A

It allows us to read individual characters

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

How do you use String.split method?

A

Define a new String:
String line = “Katie Ghaemi, 27”;

Then if we want to split by comma, we will store all of the values in a String array:
String[] tokens = line.split(“,”);

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