MIDTERM
1st Week
Exception and Assertion
Exceptions – are a mechanism used by many programming languages to describe what to do when something unexpected happens.
Common Exceptions:
NullPointerException – this is attempt to access an attribute or method of an does not refer to an object.
FileNotFoundException – this is attempt to read from a file that does not exit.
NumberFormatException – this is attempt to parse a string into a number.
ArithmeticException – this is the result of a divided-by-zero operation for integer.
SecurityException – typically thrown in browser.
Assertion – are a way to test certain assumption about the logic of a program. It checks formula.
2 Broad Categories of Exceptions:
Checked Exceptions – are those that the programmer is expected to handle in the program, and that arise from external and conditions that can readily occur in a working programs.
Example: network failure
Unchecked exceptions – might arises from conditions that represents bugs, or situations that are considered generally too difficult for a program to handle reasonably.
a. Exception class – is the base class that represents checked and unchecked exceptions.
b. Error class – is the base class used for unchecked, serious error conditions from which your program is not expected to attempt recovery.
Error – indicates a severe problem from which recovery is difficult, if not impossible.
c. RuntimeException class – is the base class that is used for the unchecked exceptions that might arises as a result of program bugs.
- indicates a design or implementation problem.
Try-Catch Statement – can be used on smaller chunks of code.
Finally Clause – defines a block of code that always executes, regardless of whether an exception was caught.
Example:
try{
startFaucet();
waterLawn();
}catch (BrokenPipeExceptions e){
logProblem (e);
} finally {