OOP Chapter 4
OOP Chapter 4
Output
Enter two integers: 5 3
5 / 3 is 1
Execution continues ...
Enter two integers: 5 0
Exception: an integer cannot be divided by zero
8/15/2023 By: Tadessse Kebede(MSc) Execution continues ... 15
Exception-handling Example 2: FileNotFoundExceptionDemo.java
ClassNotFoundException Attempt to use a class that does not exist. This exception would occur,
for example, if you tried to run a nonexistent class using the java
command, or if your program were composed of, say, three class
files, only two of which could be found.
▪ The statements in lines 7 and 10 may throw an IOException, so they are placed inside a try
block.
▪ The statement output.close() closes the PrintWriter object output in the finally block.
▪ This statement is executed regardless of whether an exception occurs in the try block or is
8/15/2023 caught. By: Tadessse Kebede(MSc) 37
Note the following:
▪ A catch clause cannot exist without a try statement.
▪ It is not compulsory to have finally clauses whenever a
try/catch block is present.
▪ The try block cannot be present without either catch clause or
finally clause.
▪ Any code cannot be present in between the try, catch, finally
blocks.