If the execution of a function can't be completed, an exception can be thrown. The most common causes of this are listed as follows:
- Invalid incoming data
- A file cannot be found
- Termination of a network connection
- Java Virtual Machine needs more available memory
The throw keyword is used to interrupt the execution of a current function and to notify a caller about an exceptional event. Let's look at the constructor of the FileInputStream class:
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file...