Lecture 3 - Exception Handling Collection Framework
Lecture 3 - Exception Handling Collection Framework
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:
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: