satish training ppt

Post on 11-Apr-2017

34 Views

Category:

Engineering

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

INDUSTRIAL TRAINING

PRESENTATION ON

EXCEPTION HANDLING IN

JAVASubmitted to :- Mr.r.s.s.rawat

(H.O.D. of it department)

Submitted by:- Satish ahirwarB.e. VIIth sem

enroll no.:-0601it131046

Contents

What is an Exception .

What happen when an Exception is occurs.

Runtime Error.

Exception class Hierarchy.

Types of Exception.

Exception Handling Keywords.Methods for printing the details of an

Exception.

What is an Exception?In general, Exception are run time errors caused due to logical mistakes occurred during program execution because of its wroung input.

Examples:-ArithmaticException.ArrayOutOfBoundException.IOException.FileNotFoundException.SQLException.etc.

Exception Example(ArithmeticException)

1 class DivByZero {2

3

public static void main(String args[]) { System.out.println(3/0);

4 System.out.println(“Pls. print me.”);5 }6 }

What Happens When an Exception Occurs?

When an exception occurs within a method, themethod creates an exception object and hands it offto the runtime system.

Exception object contains information about the error,including its type and the state of the program when theerror occurred.

Example: Default Exception Handling

Displays this error messageException in thread "main" java.lang.ArithmeticException: / by zero

at DivByZero.main(DivByZero.java:3)

Default exception handler

–Provided by Java runtime Prints out exception description Prints the stack trace

Runtime Error

It is also unwanted unexpected problem .

A run time error is occurs due to the system resource problem for run time error it is not possible to provide the solution programmatically.

Examples:-StackOverFlowErrorOutOfMemoryErrorEtc.

Exception class Hierarchy

Types Of Exception

In java there are two types of exception .

Checked Exception-the checked Exceptions are recognised by compiler.

Unchecked Exception.- The Unchecked Exceptions are not recognised by comiler.

Checked Exception

Unchecked Exception

ERROR

OutOfMemoryError

StackOverFlowErroretc,.

Exception Handling Keywords

try

catch

finally

throws

throw

syntax

try { // Risky Code } catch(type of exception-1 object-1) { // Handling Code } catch(type of exception-2 object-2) { // Handling Code} finally { // Resource releasing logic. }

throw Keyword

Java allows you to throw exceptions (generate exceptions)throw <exception object>;

An exception you throw is an object

You have to create an exception object in the same way you create any other object

Example:throw new ArithmeticException(“testing...”);

throws Keyword

Throws is a java Keyword use to by paas generated exception from present method to calling methods.

Syntax Methodname() throws <Exceptionobject>{//throw Exception object}

Methods for printing the details of an Exception.

In java there are three methods for Printing the details of an Exception

• Public void PrintstackTrace(){name of the Exception: Discription of the Exception:Location of the Exception}

• Public String toString (){Name of the Exception :Discription of the Exception.}

• Public String getmessage(){Discription of the Exception}

60

Thank You!

top related