Presentation on Exceptions in Java.ppt

download Presentation on Exceptions in Java.ppt

of 23

Transcript of Presentation on Exceptions in Java.ppt

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    1/23

    Exception Handling in Java

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    2/23

    What is an exception?

    An exception is an abnormal condition that changes the normal flowof control in a program An exception is an event, which occurs during the execution of aprogram, that disrupts the normal flow of the program's instructions

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    3/23

    T pes of Exception?

    Types of Exception 1) Checked Exception !hec"ed exceptions are chec"ed at compile#time$ The classes that

    extend Throwable class except %untimeException and Error are "nown

    as chec"ed exceptions e$g$& Exception, ()*Exception etc$2) Unchecked Exception

    The classes that extend %untimeException are "nown as unchec"edexceptions e$g$ ArithmeticException, +ull ointerException, Arra &ndex ut f-oundsException etc$ .nchec"ed exceptions are not

    chec"ed at compile#time$3) Error

    Errors are not possible to handle in our program code

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    4/23

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    5/23

    Exception Hierarch

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    6/23

    Exception Handling 0e words

    There are 1 "e words used in 2ava exception handling$tr catch

    finall throw throws

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    7/23

    tr -loc"

    Java tr bloc" is used to enclose the code that might throw an exception$ &tmust be used within the method$Java tr bloc" must be followed b either catch or finall bloc"$

    ( ntax of 2ava tr #catch

    tr 344code that ma throw exception5catch6Exception7class7+ame ref835( ntax of tr #finall bloc"

    tr 3

    44code that ma throw exception5finall 35

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    8/23

    catch -loc"

    Java catch bloc" is used to handle the Exception$ &tmust be used after the tr bloc" onl $ 9ou can use multiple catch bloc" with a single tr $

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    9/23

    finall -loc"

    Java finall bloc" is a bloc" that is used to executeimportant code such as closing connection, streametc$

    Java finall bloc" is alwa s executed whetherexception is handled or not$Java finall bloc" must be followed b tr or catch bloc"$

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    10/23

    Example of tr #catch#finall

    public class Test 3public static void main6(tring args:;83tr 3int data

    ( stem$out$println6data85catch6ArithmeticException e8

    3( stem$out$println6e8 5finall

    3( stem$out$println6@finall bloc" is alwa s executed@8 5( stem$out$println6@rest of the code$$$@85

    5

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    11/23

    throw "e word

    The Java throw "e word is used to explicitl throwan exception$ We can throw either chec"ed or unche"ed exception

    in 2ava b throw "e word$ The throw "e word ismainl used to throw custom exception$( ntax of 2ava throw "e word is given below$

    throw exception

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    12/23

    Example of throw "e word

    public class TestThrow 3static void validate6int age83if6ageB C8throw new ArithmeticException6@not valid@8

    else( stem$out$println6@welcome to vote@8

    5public static void main6(tring args:;83

    validate6 D8( stem$out$println6@rest of the code$$$@85

    5

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    13/23

    throws "e word

    The Java throws "e word is used to declare an exception$ &t gives aninformation to the programmer that there ma occur an exception so it is betterfor the programmer to provide the exception handling code so that normal flowcan be maintained$Exception Handling is mainl used to handle the chec"ed exceptions$ &f there

    occurs an unchec"ed exception such as +ull ointerException, it isprogrammers fault that he is not performing chec" up before the code beingused$

    ( ntax of 2ava throws return7t pe method7name68 throws exception7class7name3

    44method code5

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    14/23

    Exception propagation using throws "e word

    import 2ava$io$& Exceptionclass Testthrows 3 void m68throws & Exception3

    throw new & Exception6@device error@8 44chec"ed exception5

    void n68throws & Exception3m68

    5 void p683

    tr 3n685catch6Exception e83( stem$out$println6@exception handled@8 55public static void main6(tring args:;83Testthrows ob2

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    15/23

    throws "e word6contd$8

    Case1: You handle the exception&n case ou handle the exception, the code will be executed fine whether exception occurs during theprogram or not$import 2ava$io$class F3 void method68throws & Exception3

    throw new & Exception6@device error@855public class Testthrows=3

    public static void main6(tring args:;83tr 3F m

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    16/23

    throws "e word6contd$8

    You declare the exception A8&n case ou declare the exception, if exception does not occur, the code will be executed fine$-8&n case ou declare the exception if exception occures, an exception will be thrown at runtime because

    throws does not handle the exception$Progra if exception does not occur import 2ava$io$

    class F3 void method68throws & Exception3( stem$out$println6@device operation performed@8

    55class TestthrowsD3

    public static void main6(tring args:;8throws & Exception344declare exceptionF m

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    17/23

    throws "e word6contd$8

    Progra if exception occurs import 2ava$io$class F3 void method68throws & Exception3throw new & Exception6@device error@8

    55class TestthrowsG3

    public static void main6(tring args:;8throws & Exception344declare exceptionF m

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    18/23

    thro! thro!s

    Java throw "e word is used toexplicitl throw an exception$

    !hec"ed exception cannot bepropagated using throw onl $

    Throw is followed b an instance$

    Throw is used within the method$

    9ou cannot throw multipleexceptions$

    Java throws "e word is used todeclare an exception$

    !hec"ed exception can bepropagated with throws$

    Throws is followed b class$

    Throws is used with the methodsignature$

    9ou can declare multiple exceptionse$g$public void method68throws& Exception,()*Exception$

    ifference between throw and throws

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    19/23

    Exception handling with method overriding

    There are man rules if we tal" about methodoverriding with exception handling$ The %ulesare as

    followsI"f the superclass ethod does not declare an exception

    &f the superclass method does not declare an exception, subclass overridden method cannot declare the chec"ed exception but it can declare unchec"ed exception$

    import 2ava$io$class arent3 void msg683( stem$out$println6@parent@8 5

    5class !hild extends arent3 void msg68throws ArithmeticException3

    ( stem$out$println6@child@85public static void main6(tring args:;83

    arent p

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    20/23

    Exception handling with methodoverriding6!ontd$8

    "f the superclass ethod declares an exception&f the superclass method declares an exception, subclass overridden method can declare same,subclass exception or no exception but cannot declare parent exception$ import 2ava$io$class arent3 void msg68throws Exception3( stem$out$println6@parent@8 55

    class !hild extends arent3 void msg68throws ArithmeticException3( stem$out$println6@child@8 5

    public static void main6(tring args:;83arent p

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    21/23

    !ustom Exception

    &f ou are creating our own Exception that is "nown as customexception or user#defined exception$ Java custom exceptions areused to customi e the exception according to user need$- the help of custom exception, ou can have our own exceptionand message$

    Example of 2ava custom exception$

    class &nvalidAgeException extends Exception3&nvalidAgeException6(tring s83

    super6s855

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    22/23

    !ustom Exception 6contd$8

    class Test!ustomException 3

    static void validate6int age8throws &nvalidAgeException3if6ageB C8throw new &nvalidAgeException6@not valid@8

    else( stem$out$println6@welcome to vote@8

    5

    public static void main6(tring args:;83tr 3

    validate6 D8

    5catch6Exception m83( stem$out$println6@Exception occuredI @Km8 5

    ( stem$out$println6@rest of the code$$$@85

    5

  • 8/16/2019 Presentation on Exceptions in Java.ppt

    23/23

    Than" 9ou