Exceptions in Java(異常)
Errors
An Error is any unexpected result obtained from a program during execution.
Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination.
Errors should be handled by the programmer, to prevent them from reaching the user.
Some common types of program fault
Logic errors - the program does not match the specification (e.g. the requirements, or design)
Divide by zero
Exceeding array bounds
Using an uninitialised variable/object
…
Error Handling
Traditional Error Handling
Return a special value,
e.g., -1 or EoF
Shortcoming:
hard to distinguish different errors
programmer must remember the meaning of different values
Have a global error handling routine, and use some form of “jump” instruction to call this routine when an error occurs
Shortcoming:
“jump” instruction (GoTo) are considered “bad programming practice”
Exceptions in Java(異常)