Unit-4-Exception-Handling
Unit-4-Exception-Handling
NICOLAS
NEUST
At the end of the discussion, we shall be able to;
• Define exceptions;
• Use try…catch block
• Implement finally block
• Integrate throw and throws exception
An exception is an event, which occurs
during the execution of a program, that
disrupts the normal flow of the program's
instructions.
Exception handling is a core concept in
Java, allowing developers to manage
errors gracefully and maintain the flow of
a program even when unexpected
situations occur.
Create a class that divides a number by zero. Write
a try…catch block to catch the error that may
occur.
Sample computation:
int i = 56/0;
Create a class that divides a number by zero. Write
a specific try…catch block to catch the error that
may occur.
Sample computation:
int i = 56/0;
Write a try…catch block for the specific
exception and display “This isn’t possible!”.
Fill in the blanks. Supply all the missing code in the block.
Fill in the blanks. Supply all the missing code in the block.
Activity 1
You are developing a Billing System for a grocery
store. The system should allow the cashier to enter the
total amount and the amount paid by the customer.
The program should then calculate the change.
However, the following conditions must be handled using a try-catch block:
• If the cashier enters a non-numeric value for the total amount or amount
paid, the program should display:
"Invalid input! Please enter a numeric value."
• If the amount paid is less than the total amount, the program should
display:
"Insufficient payment! Please ask the customer for the correct amount.“
• Use the finally block to print “Exception successfully handled”