Exceptions and Assertions Flashcards

1
Q

ArithmeticException

A

Thrown when code attempts to divide by 0

Unchecked

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

ClassCastException

A

Thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance.

Unchecked

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

NumberFormatException

A

Thrown by the program when an attempt is made to convert a string to a numeric type, but the string doesn’t have an appropriate format.

Unchecked

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

java.text.ParseException

A

Signals that an error has been reached unexpectedly while parsing.

Checked

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

Any exception starting with java.io

A

deals with IO problems

Checked

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

Any exception starting with java.sql

A

deals with sql database problems

Checked

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

java.lang.ArrayStoreException

A

Trying to store the wrong data type in an array.

Unchecked

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

java.time.DateTimeException

A

Receiving an invalid format string for a date.

Unchecked

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

java.util.MissingResourceException

A

Trying to access a key or resource bundle that does not exist.

Unchecked

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

java. lang.IllegalStateException

java. lang. UnsupportedOperationException

A

Attempting to run an invalid operation in collections and concurrency.

Unchecked

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

How to create your own checked exception

A

class myException extends Exception {}

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

How to create your own unchecked exception

A

class myException extends RuntimeException {}

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

What are the three common constructors you can use for your custom exception?

A
public class myException extends Exception {   
   public myException() {
       super(); 
   }
   public myException(Exception e) { 
      super(e);
   }
   public myException(String message) {
      super(message); 
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to give the option to catch multiple exceptions in the same catch statement

A

Separate the different exception names by |

catch (DateTimeParseException | IOException e)

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

What is wrong with this catch statement?

catch (FileNotFoundException | IOException e)

A

FileNotFoundException is a subclass of IOException, and java will not allow this because it is redundant

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

What is wrong with this?

try {
    throw new IOException();
} catch(IOException | RuntimeException e) {
    e = new RuntimeException(); 
}
A

When using multi-catch, you cannot reassign the exception variable to something new

Note: you can do that when not using multi-catch

17
Q

When can you specify to catch a checked exception in a try block?

A

Only when it is possible for the exception to be thrown by the code, otherwise it won’t compile.

18
Q

Why would you want to use a try-with-resources statement?

A

It automatically closes all resources opened in the try clause.

public void newApproach(Path path1, Path path2) throws IOException { 
     try (BufferedReader in =  Files.newBufferedReader(path1);
BufferedWriter out = Files.newBufferedWriter(path2)) {
     out.write(in.readLine()); 
}
}
19
Q

Are catch and finally blocks required when writing a try with resources statement?

A

No

There is an implicit finally statement to close the resources, but you can also implement your own catch and finally blocks if you want.

Note: The implicit finally block runs before any explicitly coded catch or finally blocks, making the resources no longer available in those blocks!

20
Q

What is wrong with this?

try (Scanner s = new Scanner(System.in)) {  
    s.nextLine();
} catch(Exception e) {
    s.nextInt(); 
} finally{
    s.nextInt();
}
A

s has been closed by the implicit finally block so it is no longer in the scope of the catch and finally block that are coded.

21
Q

What interface must a class implement in order to be declared in a try-with-resources clause? What is the method that must be implemented?

A

AutoCloseable

close()

22
Q

Can close() throw Exception?

A

Yes, but it is recommended to throw something more specific.

And be sure that if it does, then it is handled in the calling method!

23
Q

What method does a try-with-resources statement automatically run after the try block?

A

close()

The method in the interface AutoCloseable

24
Q

What is a suppressed exception?

A

When multiple exceptions are thrown before the catch/finally blocks of a try-with-resources statement, there is one primary exception and the rest are stored.

You can loop through the suppressed exceptions like this:

for (Throwable t: e.getSuppressed())

25
Q

Which exception is thrown first at the end of this try block?

try (JammedTurkeyCage t1 = new JammedTurkeyCage(); JammedTurkeyCage t2 = new JammedTurkeyCage())

A

The exception that calling close() on t2 produces.

The exceptions stack up.

26
Q

Assertion

A

A Boolean expression that you place at a point in your code where you expect something to be true.

27
Q

Two forms of assert statement

A

assert boolean_expression;
assert boolean_expression: error_message;

The boolean expression must evaluate to true or false. It can be inside optional paren- thesis. The optional error message is a String

28
Q

What happens if an assertion is false?

A

Java throws an AssertionError and the program is ended

29
Q

How to enable assertions

A

java -enableassertions Rectangle

You can also use the shortcut -ea flag:
java -ea Rectangle

By default, this enables assertions in all classes but system classes (built in java classes)

30
Q

How to enable assertions in a specific package/subpackages

A

java -ea:package_name…

Can also do for specific class

java -ea:package_name.class_name

31
Q

What is the flag for disabling assertions?

A

-da

Can use all usual variations like when enabling

32
Q

How many catch statements can be run?

A

Only one

Even if that one throws a new exception

33
Q

Closeable

A

The interface that was used before AutoCloseable existed.

It has two key requirements:

1) Closeable restricts the type of exception thrown to IOException.
2) Closeable requires implementations to be idempotent (can be called multiple times without any side effects or undesirable behavior on subsequent runs.)