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

ExceptionHandling Part-1 in java

The document provides an overview of exception handling in Java, explaining what exceptions are, their types (checked and unchecked), and the differences between exceptions and errors. It describes the mechanisms for handling exceptions using keywords like try, catch, throw, and throws, and emphasizes the importance of exception handling in maintaining the normal flow of applications. Additionally, it includes examples of both checked and unchecked exceptions, as well as errors that cannot be recovered by handling techniques.

Uploaded by

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

ExceptionHandling Part-1 in java

The document provides an overview of exception handling in Java, explaining what exceptions are, their types (checked and unchecked), and the differences between exceptions and errors. It describes the mechanisms for handling exceptions using keywords like try, catch, throw, and throws, and emphasizes the importance of exception handling in maintaining the normal flow of applications. Additionally, it includes examples of both checked and unchecked exceptions, as well as errors that cannot be recovered by handling techniques.

Uploaded by

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

Exception

Handling in JAVA
Outline
2

◻ What is an Exception
◻ Types of Exceptions
◻ What is an Error
◻ Error vs Exception
◻ What is an Exception Handling
◻ Why we use Exception Handling
What is an Exception?
3

• Dictionary Meaning: Exception is an abnormal condition.

• An exception is an event that alters the normal flow of the program. It is


an object which is thrown at runtime.

• Exceptions are recoverable using try, catch, throw, and throws keywords.

• Derive from class Exception, said to be “thrown” and an Exception


handler “catches” it. For instance, ClassNotFoundException, IOException,
SQLException, etc.
What is Exception?
4
Types of Exceptions
5

Exceptions are divided into two categories:

• Checked Exceptions
• Known to compiler at compile time.
• Must be either handled or specified using throws keyword.
• Examples: IOException, FileNotFoundException, etc.

• Unchecked Exceptions
• Not chekced at compile time.
• Known to the compiler at runtime, also called runtime Exceptions.
• Examples: ArithmeticException, ArrayIndexOutOfBoundException,
etc.
Checked Exception (Example)
6

import java.io.*; while(( k = fis.read() ) != -1)


class Example {
{ System.out.print((char)k);
public static void main(String args[]) }
{ /*The method close() closes
FileInputStream fis = null; the file input stream * It
/*This constructor FileInputStream(File filename) throws IOException*/
throws FileNotFoundException which is a
checked exception */ fis.close();
fis = new FileInputStream("B:/myfile.txt"); }
int k; }
Output:
/* Method read() of FileInputStream class Exception in thread "main" java.lang.Error: Unresolved compilation
problems: Unhandled exception type FileNotFoundException
also throws a checked exception: IOException */ Unhandled exception type IOException
Unhandled exception type IOException
Unchecked Exception (Example)
7

// Java program illustrating exception thrown


// by AritmeticExcpetion class

public class ExceptionEg


{
public static void main(String[] args)
{ Output:
int a = 5, b = 0;
java.lang.ArithmeticException: / by zero
// Attempting to divide by zero at ExceptionEg.main(ExceptionEg.java:8)
try {
int c = a / b;
}
catch (ArithmeticException e)
{
e.printStackTrace();
} This method prints a stack trace for this Throwable
} object on the standard error output stream.
}
What is an Error?
8

• An Error “indicates serious problems that a reasonable application


should not try to catch.”

• Errors are the conditions which cannot get recovered by any handling
techniques.

• Errors belong to unchecked type and mostly occur at runtime.

• Examples: Out of memory error, a System crash error, hardware error,


StackOverflowError etc.
Error (Example)
9

// Java program illustrating stack overflow error


public class ErrorEg
// by doing infinite recursion
{
class StackOverflow
public static void main(String[] args)
{
{
public static void test(int i)
{
// eg of StackOverflowError
// No correct as base condition leads to
StackOverflow.test(5);
// non-stop recursion.
}
if (i == 0)
}
return;
else Output:
{ Exception in thread "main" java.lang.StackOverflowError
at StackOverflow.test(ErrorEg.java:7)
test(i++); at StackOverflow.test(ErrorEg.java:7)
} at StackOverflow.test(ErrorEg.java:7)
} at StackOverflow.test(ErrorEg.java:7)
} at StackOverflow.test(ErrorEg.java:7)
...
Error vs Exception
10

Error Exception

1. An error represents a condition 1. An error which reasonable


serious enough that most applications should catch.
reasonable applications should 2. Array index out of bounds
not try to catch. 3. Arithmetic errors (divide by
2. Virtual Machine Error zero
3. Out of memory 4. Null Pointer Exception
4. Stack overflow 5. I/O Exceptions
5. Thread Death
6. Linkage Error
Error vs Exception and their Class Hierarchy
11

• java.lang.Object
All Java errors implement the java.lang.Throwable, or
• java.lang.Throwable
are extended from another inherited class therein. The
• java.lang.Exception
full hierarchy of error is:
• java.lang.RuntimeException
• java.lang.Object
• java.lang.ArithematicException
• java.lang.Throwable
• java.lang.NullPointerException
• java.lang.Error
• java.lang.VirtualMachineError
• java.lang.AssertionError

The Java Virtual Machine is broken or has run out


of resources necessary for it to continue operating.
•java.lang.Object
• java.lang.Throwable
• java.lang.Exception
• java.io.IOException The AssertionError in Java is thrown when an assert
statement fails (i.e. the result is false).
What is an Exception Handling?
12

• Exception handling is a framework that is used to handle runtime


errors only, compile time errors are not handled by exception
handling in java.

• Exception handling is one of the most important feature of java


programming that allows us to handle the runtime errors caused
by exceptions.
Why we use Exception Handling?
13

To maintain the normal flow of the application.

An exception normally disrupts the normal flow of the


application that is why we use exception handling.

How we can handle the Exceptions?


14

Thanks

You might also like