Midterm 3 Flashcards

(16 cards)

1
Q

Run-time error

A
  • Illegal/impossible operations to execute
  • Detected during program execution
    • -> but not detectable at compile time
  • treated as exceptions in java

ex.
1. division by zero
2. using null reference
3. illegal format conversion

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

What to do when error occur

A

Alternatives
• 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

Exceptions

A

• Rare event outside normal behavior of code
–> Usually a run-time error
• Examples
-Division by zero • Access past end of array • Out of memory
-Number input in wrong format (integer vs. float)
-Unable to write output to file
-Missing input file
-Application specific
–>(e.g., attempt to remove a nonexistent customer from a database)

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

Exceptions

A

• Notice that you can also define your own exceptions and
generate them when your code detects errors
• When any of the previous errors occur we say that “an exception
occurred”
• We use the phrase “a program throws an exception” to indicate a program generates 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
5
Q

What to do when Exception occurs? (nothing)

A

• Usually, your program will be terminated by the JVM • Stack trace is printed showing where exception was generated
->Red and blue in Eclipse window
Example: MilesPerGallon.java

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

What to do when Exception occurs? (do something about it)

A

• 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
–>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
• Example: MilesPerGallonV2.java

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

What to do when an exception occurs?

A

• Notice that 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
• Exception Object
–> When an exception is thrown, a new exception object is created, which
encodes information about the nature of the exception
• Every Exception object supports the following methods:

• Exception(String message)
–> Constructor taking an explanation as an argument

  • String getMessage() –> Returns a message describing the exception
  • void printStackTrace() –> 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
8
Q

Exception Types

A
  1. ArithmeticException
  2. NullPointerException
  3. IndexOutOfBoundsException
  4. ArrayStoreException
  5. EmptyStackException
  6. IOException
  7. NumberFormatException
  8. Exception
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ArithmeticException

A

divide by zero

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

EmptyStackExceptio

A

attempt to pop an empty Stack (java.util).

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

IOException

A

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

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

NumberFormatException

A

attempt to convert an invalid string into a

number (e.g., when calling Integer.parseInt( ) ) …

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

Exception

A

The most generic type of exception.