exceptions Flashcards

1
Q

run-time errors

A

> Illegal/impossible operations to execute
Detected during program execution
But not detectable at compile time
Treated asexceptionsin Java
(examples: division by zero, using null reference, illegal format conversion)

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

what to do when an error occurs…

alternatives

A

> Ignore error
Print error message and terminate
Have the method handle the error in the code where the problem lies as best as you can
Have the method pass it off to someone else to handle
Usually an error code is returned to whoever called the method

> Modern language approach:Exception!
(note: in language like C, a “core dump” takes place)

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

how to indicate when a program generates an exception

A

“a program throws an exception”

In Java an exception is represented by an object

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

what to do when an exception occurs…

A

do nothing:
>Usually your program will be terminated by the JVM
>Stack trace is printed showing where exception was generated (Red and blue in Eclipse window)

do something:
>In some applications aborting the program is not an option (Email processor, web/database server)
> We can “handle” the exception by “catching the exception” and defining code to be executed

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

try

catch

A

Use try { } to enclose code that can potentially throw an exception

Use catch(EXCEPTIONTYPE e) { } to “catch” the exception and define the code to execute when the exception occurs

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

why is try catch somewhat similar to if else

A

> when an exception occurs control “jumps” to the catch clause and code after the point where the exception occurred is not executed
If the exception is not thrown the code of the catch clause is ignored

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

exception object

A

When an exception is thrown, a new exception object is created, which encodes information about the nature of the exception

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

Exception(String message)

A

Constructor taking an explanation as an argument

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

String getMessage()

A

Returns a message describing the exception

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

void printStackTrace()

A

Prints the contents of the Java call stack at the time of the exception

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

ArithmeticException

A

divide by zero

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

NullPointerException

A

attempt to access an object with a null reference

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

IndexOutOfBoundsException

A

array or string index out of range

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

ArrayStoreException

A

attempting to store an object of type X in an array of type Y

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

EmptyStackException

A

attempt to pop an empty Stack (java.util)

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

IOException

A

attempt to perform an illegal input/output operation (java.io)

17
Q

NumberFormatException

A

attempt to convert an invalid string into a number (e.g., when calling Integer.parseInt( ) )

18
Q

Exception

A
The most generic type of exception.
All of these are Objects in Java’s class library (most in java.lang)
Every Exception object supports a group of methods (these methods are defined in this stack of flashcards)
19
Q

exception propagation

A

(looks for exception handlers)
Java pops back up the call stack to each of the calling methods to see whether the exception is being handled in a catch block of the method

20
Q

what happens when your code does not catch your exceptions

A

java aborts your program

21
Q

try

finally

A

similar to try catch, however finally always runs (whether there is an error or not)
(For example if the return occurs before the finally clause, the clause will be executed and then we will return)

22
Q

write your own exceptions…

A

> You can throw exceptions using throw

>Never leave the catch clause empty (when in doubt call printStackTrace)