0% found this document useful (0 votes)
4 views

Lecture 3 - Exception Handling Collection Framework

Uploaded by

tajrianaltahrim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 3 - Exception Handling Collection Framework

Uploaded by

tajrianaltahrim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 3

Kazi Rifat Ahmed


Lecturer
Department of Software Engineering
Daffodil International University
Exception Handling & Collection
Framework
What is Exception
➔ In Java, Exception is an unwanted or unexpected event, which occurs
during the execution of a program, i.e. at run time, that disrupts the
normal flow of the program’s instructions.
➔ Exceptions can be caught and handled by the program.
➔ When an exception occurs within a method, it creates an object.
➔ This object is called the exception object.
Major reasons why an exception
occurs:
➔ Invalid user input
➔ Device failure
➔ Loss of network connection
➔ Physical limitations (out-of-disk memory)
➔ Code errors
➔ Opening an unavailable file
Difference between Error and
Exception
● Error: An Error indicates a serious problem that a reasonable
application should not try to catch.
● Exception: Exception indicates conditions that a reasonable application
might try to catch.
Exception Hierarchy
Exception Example
import java.io.*;

class GFG {
public static void main (String[] args) {
int a=5;
int b=0;
try{ Output
System.out.println(a/b); java.lang.ArithmeticExcept
} ion: / by zero
catch(ArithmeticException e){ at GFG.main(File.java:10)
e.printStackTrace();
}
}
}
Java try…..catch Block
The try-catch block is used to handle exceptions in Java. Here's the syntax of
try...catch block:
try {
// code
}
catch(Exception e) {
// code
}
Example: Exception handling using
try...catch
class Main {
public static void main(String[] args) {
try {
// code that generate exception
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block"); Output:
} ArithmeticException => / by zero

catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
}
}
Java finally Block
In Java, the finally block is always executed no matter whether there is an
exception or not.
The finally block is optional. And, for each try block, there can be only one
finally block.
try {
//code
}
catch (ExceptionType1 e1) {
// catch block
}
finally {
// finally block always executes
}
Example: Java Exception Handling using
finally block
class Main {
public static void main(String[] args) {
try {
// code that generates exception
int divideByZero = 5 / 0;
} Output:

ArithmeticException => / by zero


catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage()); This is the finally block
}

finally {
System.out.println("This is the finally block");
}
}
}
Collection in Java
➔ The Collection in Java is a framework that provides an architecture to
store and manipulate the group of objects.
➔ Java Collections can achieve all the operations that you perform on a data
such as searching, sorting, insertion, manipulation, and deletion.
Framework in Java
○ It provides readymade architecture.
○ It represents a set of classes and interfaces.
○ It is optional.
What is Collection Framework
The Collection framework represents a unified architecture for storing and
manipulating a group of objects. It has:

1. Interfaces and its implementations, i.e., classes


2. Algorithm
Hierarchy of Collection Framework
References
1. https://www.programiz.com/java-programming/exception-handling
2. https://www.javatpoint.com/collections-in-java
3. https://www.javatpoint.com/exception-handling-in-java
Thank You

You might also like