Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Exception Handling in Java Programming - Prof. Guerra, Slides of Java Programming

This will help you to see some examples in exception handling

Typology: Slides

2020/2021

Uploaded on 04/01/2022

zurbito-jacque-landrito
zurbito-jacque-landrito 🇵🇭

5 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
”Be a Gabrielian: We Care, your Legacy to Share”
BITE3 – Programming 2
Bachelor of Science in Information Technology
Exception Handling
Presented by
JIMMY DE VERA ROLDAN, MSIT
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Exception Handling in Java Programming - Prof. Guerra and more Slides Java Programming in PDF only on Docsity!

BITE3 – Programming 2 Bachelor of Science in Information Technology

Exception Handling

Presented by

JIMMY DE VERA ROLDAN, MSIT

Specific Objectives

At the end of the topic session, the student should be able to:

  1. Recognize the importance of exception handling; and
  2. Write programs that handle exceptions correctly.

Exception

An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Here are sample exceptions:

  • (^) When a program issues a command to read a file from a disk but the file does not exist there.
  • (^) When a program attempts to write a data to a disk but the disk is full.
  • (^) When a program asks for a user input but the user enters an input of invalid data type.

Exception

  • (^) When a program attempts to divide a value by zero (0).
  • (^) When a program tries to access an array with an illegal index.

Checked 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.

Checked Exceptions

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.

Unchecked Exceptions

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.

Unchecked Exceptions

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.

catch Block

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 }

Programming Exercise 1

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).

finally Block

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

Exception Handling

Presented by

JIMMY DE VERA ROLDAN, MSIT