









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This will help you to see some examples in exception handling
Typology: Slides
1 / 17
This page cannot be seen from the preview
Don't miss anything!
BITE3 – Programming 2 Bachelor of Science in Information Technology
At the end of the topic session, the student should be able to:
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Here are sample exceptions:
The exceptions may occur at either compilation or execution. Exceptions that occur during compilation are called checked exceptions. These exceptions cannot be ignored and should be resolved to be able to execute the program.
Here are the most common checked exceptions in Java: Exception Description ClassNotFoundException The class is not found. IllegalAccessException Access to a class is denied. InstantiationException Attempt to create an object of an abstract class or an inheritance. NoSuchMethodException A request method does not exist.
The most common unchecked exceptions are: Exception Description ArithmeticException Arithmetic error such as an integer divided by zero (0). ArrayIndexOutOfBoundsException Accessing an invalid index of the array. ArrayStoreException Assigning a value to an array index that does not match the expected data type. InputMismatchException Entering a value that does not match the expected data type.
Exception Description NullPointerException Invalid use of a null reference. NumberFormatException Invalid conversion of a string to a numeric format. StringIndexOutOfBoundsException Accessing an invalid index (character) of a string.
A catch block is a segment of code that can handle an exception that might be thrown by the try block that precedes it. Below is the format for the catch block: catch (Exception exceptionName) { // actions to take if exception occurs }
Create a program that will ask the user to enter 2 numbers. Divide the first number by the second number and display the result. Generate an exception if the second number is zero (0).
You can add a finally block a sequence of catch blocks. The finally block contains statements which are executed whether or not an exception is thrown. Below is the format for the finally block: finally { // statement or statements that will be executed }
BITE3 – Programming 2 Bachelor of Science in Information Technology