0% found this document useful (0 votes)
20 views9 pages

Oop L21

Exception handling in Java allows the normal flow of a program to continue after exceptions or errors occur. Exceptions are unwanted events that disrupt normal program flow, while errors are serious problems outside program control. Common exceptions include invalid user input or device failures. Exceptions are handled by catching them, while errors typically cannot be caught. Checked exceptions must be declared or handled, unlike unchecked exceptions.

Uploaded by

Jaswanth Kumar
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)
20 views9 pages

Oop L21

Exception handling in Java allows the normal flow of a program to continue after exceptions or errors occur. Exceptions are unwanted events that disrupt normal program flow, while errors are serious problems outside program control. Common exceptions include invalid user input or device failures. Exceptions are handled by catching them, while errors typically cannot be caught. Checked exceptions must be declared or handled, unlike unchecked exceptions.

Uploaded by

Jaswanth Kumar
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/ 9

OOP L21

Exception Handling
• Exception Handling in Java is one of the effective means to handle runtime errors so
that the regular flow of the application can be preserved.
• 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.
• When an exception occurs within a method, it creates an object. This object is called
the exception object.
• It contains information about the exception, such as the name and description of the
exception and the state of the program when the exception occurred.
• Exceptions can be caught and handled by the program.
• Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of
the application. An exception normally disrupts the normal flow of the
application; that is why we need to handle exceptions. Let's consider a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9; With appropriate exception handling, the
statement 10; statements from 6-10 will be executed.
• 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
Hierarchy of Java Exception classes
The java.lang.Throwable class is the root class of Java Exception hierarchy
inherited by two subclasses: Exception and Error.
• Checked Exceptions: Checked exceptions are called compile-time
exceptions because these exceptions are checked at compile-time by
the compiler.
• Unchecked Exceptions: The unchecked exceptions are just opposite to
the checked exceptions. The compiler will not check these exceptions
at compile time. In simple words, if a program throws an unchecked
exception, and even if we didn’t handle or declare it, the program
would not give a compilation error.
• Example of Checked Exception:
import java.io.*;
class GFG {
public static void main(String[] args)
{
FileReader file = new FileReader("C:\\test\\a.txt");
BufferedReader fileInput = new BufferedReader(file);

// Printing first 3 lines of file "C:\test\a.txt"


for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());

// Closing file connections


// using close() method
fileInput.close();
}
}
• Example of unchecked Exception:
class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
}
}
Errors
• Errors are usually caused by serious problems that are outside the
control of the program, such as running out of memory or a system
crash.
• Errors are represented by the Error class and its subclasses. Some
common examples of errors in Java include:
• OutOfMemoryError: Thrown when the Java Virtual Machine (JVM)
runs out of memory.
• StackOverflowError: Thrown when the call stack overflows due to too
many method invocations.
• NoClassDefFoundError: Thrown when a required class cannot be
found.

You might also like