Department of Computer Science and Application
School of Computer Science and Engineering
Title : “ Exception Handling In Java ”
Advance Java
Academic Year : 2023 - 2024
Presented By :
Name : Lubna Javed
Mansoori
Class : FY MCA (sem II)
PRN : 230105051137
Presented To :
Dr. Jayshree Kunde
Assistant Professor
SOCOEA,SUN
INTRODUCTION
What is Exception Handling ?
Exception handling is the process of responding to the occurrence of exceptions,
which are unexpected events that disrupt the normal flow of program execution.
Importance of Exception Handling
Proper exception handling improves
 Program reliability
 Maintainability
 user experience.
This presentation will cover the basics
of Java exception handling, including
types of exceptions, try-catch blocks,
throwing exceptions, and best
practices.
TYPES OF EXCEPTION
Checked Exception
• Compile-Time Verification:
This exceptions are verified by
the compiler at compile time,
ensuring that they are either
caught or declared in the method
signature.
• Forced Handling: Developers
are compelled to handle checked
exceptions explicitly, either by
enclosing risky code within try-
catch blocks or by declaring them
in the method signature using the
throws keyword.
• Examples:
IOException,
SQLException
Unchecked Exception
• Runtime Exception Subclass:
Unchecked exceptions in Java are
subclasses of RuntimeException or
Error, or their subclasses, making
them unchecked by the compiler.
• Not Enforced by Compiler:
Unlike checked exceptions,
unchecked exceptions are not
enforced by the compiler, meaning
they do not need to be caught or
declared.
• EXAMPLE :
NullPointerException
ArrayIndexOutOfBoundsException
Error
• Irrecoverable Conditions:
Errors in Java represent
irrecoverable conditions which
occur due to fundamental
problems with the environment or
resources, make it impossiblento
continue program execution.
• Not Intended for Normal
Exception Handling: Unlike
exceptions, errors are not
intended to be caught or handled
by application code. They are
usually serious issues that
indicate severe problems in the
runtime environment or the JVM
itself.
• Example :
OutOfMemoryError
StackOverflowError
try {
// Risky code that may throw an exception
} catch (ExceptionType1 e1) {
// Handler for ExceptionType1
} catch (ExceptionType2 e2) {
// Handler for ExceptionType2
} finally {
// Optional block executed regardless of whether an
exception occurred }
Try-Catch BLOCKS
Explanation: The try block contains the code that
may throw an exception. If an exception occurs,
control transfers to the appropriate catch block. The
finally block is executed regardless of whether an
exception occurred, typically used for cleanup tasks.
public void methodName() throws SomeException
{
// Code that may throw SomeException
if (/* condition */)
{
throw new SomeException("Error message");
}
}
THROWING EXCEPTIONS
Explanation:A method can declare that it throws
an exception using the "throws" keyword in its
signature. Inside the method, the "throw" keyword
is used to throw an instance of the specified
exception class.
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e)
{ System.out.println("Error: Division by zero"); }
}
public static int divide(int dividend, int divisor)
{ return dividend / divisor; }
}
THROWING EXCEPTIONS
Explanation:In this example, the divide method
attempts to perform division. If the divisor is zero,
an ArithmeticException is thrown and caught in the
main method's catch block.
BEST PRACTICES
 Handle exceptions at an appropriate level of abstraction.
 Use specific exception types rather than catching generic
exceptions.
 Log exceptions with meaningful messages for debugging and
troubleshooting.
 Clean up resources in the finally block to ensure proper resource
management.
 Avoid catching errors or exceptions that you cannot handle
effectively.
CONCLUSION
 Java exception handling is a vital aspect of writing robust and
reliable software.
 By understanding types of exceptions, using try-catch blocks, and
following best practices, developers can create more resilient and
maintainable code.
 Practice and experience are key to mastering exception handling in
Java.
Thank You

presentationon exception handling in java.pptx

  • 1.
    Department of ComputerScience and Application School of Computer Science and Engineering Title : “ Exception Handling In Java ” Advance Java Academic Year : 2023 - 2024 Presented By : Name : Lubna Javed Mansoori Class : FY MCA (sem II) PRN : 230105051137 Presented To : Dr. Jayshree Kunde Assistant Professor SOCOEA,SUN
  • 2.
    INTRODUCTION What is ExceptionHandling ? Exception handling is the process of responding to the occurrence of exceptions, which are unexpected events that disrupt the normal flow of program execution. Importance of Exception Handling Proper exception handling improves  Program reliability  Maintainability  user experience. This presentation will cover the basics of Java exception handling, including types of exceptions, try-catch blocks, throwing exceptions, and best practices.
  • 3.
    TYPES OF EXCEPTION CheckedException • Compile-Time Verification: This exceptions are verified by the compiler at compile time, ensuring that they are either caught or declared in the method signature. • Forced Handling: Developers are compelled to handle checked exceptions explicitly, either by enclosing risky code within try- catch blocks or by declaring them in the method signature using the throws keyword. • Examples: IOException, SQLException Unchecked Exception • Runtime Exception Subclass: Unchecked exceptions in Java are subclasses of RuntimeException or Error, or their subclasses, making them unchecked by the compiler. • Not Enforced by Compiler: Unlike checked exceptions, unchecked exceptions are not enforced by the compiler, meaning they do not need to be caught or declared. • EXAMPLE : NullPointerException ArrayIndexOutOfBoundsException Error • Irrecoverable Conditions: Errors in Java represent irrecoverable conditions which occur due to fundamental problems with the environment or resources, make it impossiblento continue program execution. • Not Intended for Normal Exception Handling: Unlike exceptions, errors are not intended to be caught or handled by application code. They are usually serious issues that indicate severe problems in the runtime environment or the JVM itself. • Example : OutOfMemoryError StackOverflowError
  • 4.
    try { // Riskycode that may throw an exception } catch (ExceptionType1 e1) { // Handler for ExceptionType1 } catch (ExceptionType2 e2) { // Handler for ExceptionType2 } finally { // Optional block executed regardless of whether an exception occurred } Try-Catch BLOCKS Explanation: The try block contains the code that may throw an exception. If an exception occurs, control transfers to the appropriate catch block. The finally block is executed regardless of whether an exception occurred, typically used for cleanup tasks.
  • 5.
    public void methodName()throws SomeException { // Code that may throw SomeException if (/* condition */) { throw new SomeException("Error message"); } } THROWING EXCEPTIONS Explanation:A method can declare that it throws an exception using the "throws" keyword in its signature. Inside the method, the "throw" keyword is used to throw an instance of the specified exception class.
  • 6.
    public class ExceptionHandlingExample{ public static void main(String[] args) { try { int result = divide(10, 0); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("Error: Division by zero"); } } public static int divide(int dividend, int divisor) { return dividend / divisor; } } THROWING EXCEPTIONS Explanation:In this example, the divide method attempts to perform division. If the divisor is zero, an ArithmeticException is thrown and caught in the main method's catch block.
  • 7.
    BEST PRACTICES  Handleexceptions at an appropriate level of abstraction.  Use specific exception types rather than catching generic exceptions.  Log exceptions with meaningful messages for debugging and troubleshooting.  Clean up resources in the finally block to ensure proper resource management.  Avoid catching errors or exceptions that you cannot handle effectively.
  • 8.
    CONCLUSION  Java exceptionhandling is a vital aspect of writing robust and reliable software.  By understanding types of exceptions, using try-catch blocks, and following best practices, developers can create more resilient and maintainable code.  Practice and experience are key to mastering exception handling in Java.
  • 9.