0% found this document useful (0 votes)
32 views19 pages

Exception Handling Collection Framework

The document provides an overview of exception handling and the collection framework in Java, explaining what exceptions are, their causes, and the difference between errors and exceptions. It details the use of try-catch blocks, finally blocks, and custom exceptions, along with examples of each. Additionally, it introduces the Java Collection Framework, highlighting its architecture, interfaces, and algorithms for managing groups of objects.

Uploaded by

jabikoi314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views19 pages

Exception Handling Collection Framework

The document provides an overview of exception handling and the collection framework in Java, explaining what exceptions are, their causes, and the difference between errors and exceptions. It details the use of try-catch blocks, finally blocks, and custom exceptions, along with examples of each. Additionally, it introduces the Java Collection Framework, highlighting its architecture, interfaces, and algorithms for managing groups of objects.

Uploaded by

jabikoi314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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.
Error Example: Out of Memory Error
import java.util.ArrayList;
import java.util.List;

public class OutOfMemoryErrorExample {


public static void main(String[] args) {
List<int[]> memoryHog = new ArrayList<>();
while (true) {
memoryHog.add(new int[1_000_000]); // Allocates a large
array continuously
}
}
}
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");
}
}
}
What Are Custom Exceptions?
● Custom exceptions allow developers to define their own exception types for
specific application needs.
● Useful for handling application-specific errors more effectively.
● Created by extending the Exception or RuntimeException class.

// Custom Exception Class


class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
class CustomException extends Exception {
Age Example
public CustomException(String message) {
super(message);
}
}

class Main {
public static void validateAge(int age) throws CustomException
{
if (age < 18) {
throw new CustomException("Age must be 18 or older.");
}
}

public static void main(String[] args) {


try {
validateAge(16);
} catch (CustomException e) {
System.out.println("Exception caught: " +
e.getMessage());
}
}
}
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
•Predefined Architecture: •Inversion of Control (IoC):
A framework provides a standard The framework controls the flow of the
structure for developers to build application rather than the developer
applications, reducing the need to write writing the entire flow manually.
boilerplate code. •Best Practices and Design Patterns:
Frameworks enforce best practices and
•Reusability: design patterns, improving code quality
Developers can use pre-built classes, and maintainability.
interfaces, and methods to handle •Libraries and Tools:
common tasks like database They come with a rich set of tools and
connectivity, web development, or libraries for tasks such as logging,
security. testing, and data handling.
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