This video will introduce the Catch or Specify Requirement, which is a requirement for checked exceptions.
The following code may have been altered to comply with youtube's video description policy. To download the actual files, go here:
• Java Exception Handling 00. Setting up and...
02. Catch or Specify Requirement
---------------
Every exception object that gets thrown inherits from the Throwable class.
Object
|
Throwable
| |
Error Exception
| |
... |-------|
... RuntimeException
The Catch or Specify Requirement states that the code in which an Exception object* is thrown MUST be enclosed in either:
1) A try statement. This statement must provide a handler for the exception.
ex. try { ... } catch (Exception ex) { ... }
2) A method that states that it can throw the exception.
ex. public static void readFile() throws IOException { ... }
*This does not include RuntimeExceptions
Let's talk about checked and unchecked exceptions
Checked Exceptions
Exception class, but not the RuntimeException
Subject to the Catch or Specify Requirement - we have to catch it before our program compiles.
Conditions that a well-written program should anticipate.
Such as if it reads from a file, and the user inputs a file that doesn't exist.
ex. class or variable not found, IO exceptions
Unchecked Exceptions
Error class
External to the application.
Not really your fault as the programmer. (nothing wrong with your code)
Hardware or system malfunctions.
Can't do much except notify the user and close the program gracefully.
Not subject to the Catch or Specify Requirement.
ex. linkage errors (class dependencies no longer valid)
RuntimeException class
Within your programming.
Logic errors, improper use of an API - calling a method that doesn't exist.
ex. Indexing an array out of bounds, or dividing by zero