unit 6 - exception handling

40
7/23/2019 Unit 6 - Exception Handling http://slidepdf.com/reader/full/unit-6-exception-handling 1/40 Basic Java Copyright © 2008 Pratian Technologies www.pratian.com Unit 6 – !ception "an#ling Pratian Technologies $%n#ia& Pvt. 't#. www.pratian.com Basic Java Unit 6 – !ception "an#ling

Upload: mee-nu

Post on 17-Feb-2018

242 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 1/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Pratian Technologies $%n#ia& Pvt. 't#.www.pratian.com

Basic Java

Unit 6 – !ception "an#ling

Page 2: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 2/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Topics

Ø rror "an#ling in Conventional 'ang(agesØ !ceptionsØ !ception "an#ling in Java

Ø !ception "ierarchyØ Throwa)le ClassØ rror an# !ceptionØ Types o* !ceptionsØ Throwing !ceptionsØ

+eclaring !ceptions per ,etho#Ø Catching !ceptionØ +esigning (ser-#e*ine# !ception classes

Ø ,etho# verri#ing an# !ceptions

Page 3: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 3/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

rror Con#ition§ (r applications sho(l# )e )(ilt to s(rvive /error

con#itions.

§ The program 1 mo#(le m(st han#le the error sit(ation

grace*(lly an# not (st terminate a)r(ptly

3hat /error are we tal4ing a)o(t 5

Page 4: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 4/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

rror Con#ition§  n error is a con#ition #(e to which a program cannot

normally contin(e e!ec(tion an# nee#s to )e a)r(ptly

terminate#.

Page 5: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 5/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

rror han#ling in tra#itional lang(ages§ Tra#itional error han#ling metho#s incl(#e

§ Boolean *(nctions $which ret(rn T7U1'9&.

§ %nteger *(nctions $ret(rns –: on error&.§  n# other ret(rn arg(ments an# special val(es.

int main () {

  int res;

  if (can_fail () == -1) {  cout << "Something failed!" << endl;

  return 1;

  }

  if(div(1res) == -1) {

  cout << "ivision #$ %ero!" << endl;  return &;

  }

 return ;

  }

Can %

(n#erstan#

what wentwrong with

 (st the

ret(rn

val(e5

 m % *orce#

to receive

theret(rning

val(e *rom a

*(nction5

%s error

han#ling

#one in aseries o* i*

then )loc45

%s my co#emaintaina)l

e5

Page 6: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 6/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Pro)lems with tra#itional approach

  Caller is not *orce# to

receive the val(e

ret(rne# )y the *(nction

  (nction can ret(rn only

one val(e ; not #etails

o* the error con#ition   ,(ltiple i*-else lea#s tocl(ttere# co#e ; is

#i**ic(lt to maintain

Page 7: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 7/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

!ception han#ling in Java§ Javas s(pport *or error han#ling is #one thro(gh

!ceptions.

§ 'hat is an ce*tion+§  n exception  is an event that occ(rs #(ring the e!ec(tion

o* a program that #isr(pts the normal *low o* instr(ctions.

§ %n the wa4e o* s(ch an event< the J=, creates an

e!ception o)ect< that contains in*ormation a)o(t the error<

incl(#ing its type an# state o* the program when the error

occ(rre#.

§ Creating an e!ception o)ect an# noti*ying the caller o*

the metho# is calle# throwing an exception.

Page 8: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 8/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

!ception han#ling mechanism

,ain

,etho# where

error occ(rre#

,etho# Call

Throws

e!ception

'oo4ing *or

appropriate han#ler 

9ample 'isting> ivisionrroremo,ava

Page 9: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 9/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

3hy (se e!ceptions5§ !ception han#ling provi#es the programmer with

many a#vantages over tra#itional error-han#ling

techni?(es>

Can %

(n#erstan#what went

wrong an#

where when

an error is

thrown5

 m % *orce#

to han#lee!ceptions5

%s error

han#ling#one in a

series o* i*

then )loc45

%s my co#e

maintaina)l

e5

%s it easy to

(n#erstan#

how to

han#le

e!ceptions5

Page 10: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 10/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

To s(mmari@e a#vantages o* !ception

han#ling§ !ception han#ling provi#es the programmer with

many a#vantages over tra#itional error-han#ling

techni?(es>

§ %t provi#es a means to separate error-han#ling co#e *rom

*(nctioning program co#e.

§ %t provi#es a mechanism *or propagating errors (p the

metho# call stac4< meaning that metho#s higher (p the call

chain can )e allowe# to han#le pro)lems originating lower

in the chain.

§ %t provi#es a way to organi@e an# #i**erentiate )etween

#i**erent types o* a)normal con#itions.

§ Both< application logic an# pro)lem han#ling logic< )ecome

simpler< cleaner an# clearer.

Page 11: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 11/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Throwa)le class§ 3hen an error occ(rs in a metho# the caller o* the metho# is noti*ie#

)y throwing an e!ception o)ect< this o)ect sho(l# )e o* the typeclass Throwable.

§ Throwable has two s()classes< Error  an# Exception.

.#ect

/hro0a#le

ce*tionrror 

untimece*tion

Page 12: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 12/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

rrors

§ rrors are e!ceptional con#itions that are e!ternal to theapplication< an# that the application cannot anticipate or recover

*rom< there*ore rrors are not handled #$ the a**lication.§ or e!ample<

§  n application has to open a *ile an# write to it< #(e to somehard0are error < the application is (na)le to open the *ile we wo(l#get a ava.io.%rror.

Page 13: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 13/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Types o* !ception

.#ect

/hro0a#le

ce*tionrror 

untimece*tion

Page 14: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 14/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Types o* !ception

§ !ceptions are o* 2 types§ 2hec3ed ece*tions 

§ 4nchec3ed or runtime ece*tion

Page 15: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 15/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Types o* !ception

§ 2hec3ed ece*tions § These are e!ceptional con#itions that a well written application

sho(l# anticipate an# recover *rom.§ 9ince these e!ceptions are anticipate# an# are recovera)le

con#itions< com*iler forces these e!ceptions to )e han#le#.

  e.g.> ileAoto(n#!ception< %!ception< 9'!ception

§ 4nchec3ed or runtime ece*tion§

These are e!ceptional con#itions that are internal to theapplication< an# that the application cannot anticipate or recover*rom.

§ These (s(ally in#icate programming )(gs< s(ch as logical errors orimproper (se o* an P%.

§ 9ince these can )e *i!e# programmatically< com*iler does not

force these e!ceptions to )e han#le#.  e.g.> A(llPointer!ception< rray%n#e!(t*Bo(n#s!ception

Page 16: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 16/40Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

"ow #o % han#le e!ceptions5

§ Ao nee# to import any ava pac4age§ Java programming lang(age s(pports< e!ception han#ling

)y provi#ing three e!ception han#ler components< these

are§ try § catch

§ finally 

§ The try < catch  an# finally  )loc4s are (se# to write an

e!ception han#ler.

Page 17: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 17/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

try Bloc4

tr$

  co#e

  DDD..

E

try )loc4

§  ny co#e that might throw an e!ception is enclose#within a try  )loc4. %t is the *irst step in constr(cting an

e!ception han#ler.

U it 6 ti " #li

Page 18: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 18/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

try Bloc4 !ample

p()lic int co(ntChars$9tring *ileAame&

  int total F 0G  try

  ile7ea#er r F new ile7ea#er$*ileAame&G

  while$ r.rea#y$&&

  r.rea#$&G  totalHHG

  E

  r.close$&G

E

E

U it 6 ti " #li

Page 19: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 19/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

catch Bloc4§ The catch  )loc4 contains

co#e that is e!ec(te# i* an#when the e!ception occ(rs.

§   catch )loc4 is an e!ceptionhan#ler associate# with a try  )loc4 an# han#les the type o*e!ception in#icate# )y its

arg(ment.

§ very try )loc4 is associate#with zero or more catchblock 

try

E

catch$ExceptionType name&

Ecatch$ExceptionType name&

E

Unit 6 ception "an#ling

Page 20: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 20/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

catch Bloc4 !ample

p()lic int co(ntChars$9tring *ileAame&

  int total F 0G  try

  ile7ea#er r F new ile7ea#er$*ileAame&G

  while$ r.rea#y$&&

  r.rea#$&G

  totalHHG

 E  r.close$&G

  E

  catch$ileAoto(n#!ception e&

  9ystem.o(t.println$Iile name# I H *ileAame H Inot *o(n#. I He&G

  total F -:G

  E  catch$%!ception e&

  9ystem.o(t.println$Una)le to per*orm %1< please try laterK&G

  total F -:G

  E

E

Unit 6 !ception "an#ling

Page 21: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 21/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Try Catch Bloc4 !ample

*u#lic static dou#le divide(int a int #)

{

dou#le 5uotient=;

tr${

5uotient=a6#;

} catch (7rithmeticce*tion e){ S$stem,out,*rintln(" ivisor is 8ero");}

 return 5uotient;

}

Unit 6 !ception "an#ling

Page 22: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 22/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

Try catch Bloc4 !ample*u#lic static void main(String9: args)

{

int a=;

int #=;

tr${

a=nteger,*arsent(args9:);

#=nteger,*arsent(args91:);}

  catch (um#erormatce*tion e)

{

S$stem,out,*rintln(" >lease enter an integer onl$ ,,,");

}

dou#le ans=divide(a#);

S$stem,out,*rintln("/he 5uotient is "? ans);

}

9ee listing > @andledivisionrroemo,ava

Unit 6 !ception "an#ling

Page 23: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 23/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

!ercise§ 3rite some co#e that is capa)le o* generating e!ceptions

li4e rithmetic!ception< A(m)erormat!ception<

 rray%n#e!(t*Bo(n#s!ception etc

3rite an e!ception han#ler< that is< enclose these line o*

co#e in try )loc4 an# write appropriate catch )loc4s to catch

#i**erent e!ception types.

Unit 6 – !ception "an#ling

Page 24: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 24/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 – !ception "an#ling

!ception "an#ler § %t is possi)le to write a generic catch )loc4 to catch any

type o* e!ception that might occ(r 

p()lic int co(ntChars$9tring *ileAame&

  int total F 0G

try

  ile7ea#er r F new ile7ea#er$*ileAame&G

  while$ int c F r.rea#$& LF -:&

  totalHHGr.close$&G

  E

  catch$ileAoto(n#!ception e&

  9ystem.o(t.println$Iile name# I H *ileAame H Inot *o(n#. I He&G

  E

  catch$%!ception e&  9ystem.o(t.println$Una)le to per*orm %1< please try laterK&G

  E

  catch$!ception e&

  9ystem.o(t.println$Una)le to contin(e processing< #(e to

some internal errorK&G

  E

3henever an

!ception other than

ileAoto(n# or 

%!ception occ(r<

this catch )loc4 isinvo4e#

9(ch a generic catch

)loc4 sho(l# )e

incl(#e# at the en#<

a*ter the speci*ic

catch )loc4s

Unit 6 – !ception "an#ling

Page 25: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 25/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 !ception "an#ling

Clean (p co#e

p()lic int co(ntChars$9tring *ileAame&

  int total F 0G  try

  ile7ea#er r F new ile7ea#er$*ileAame&G

  while$ r.rea#y$&&

  r.rea#$&G

  totalHHG

 E r.close$&G

  E

  catch$ileAoto(n#!ception e&

  9ystem.o(t.println$Iile name# I H *ileAame H Inot *o(n#. I He&G

  total F -:G

 r.close$&G  E

  catch$%!ception e&

  9ystem.o(t.println$Una)le to per*orm %1< please try laterK&G

  total F -:G

 r.close$&G

  E

+eveloper nee#s to

per*orm clean (p

that is< release the

*ile that was opene#

programmatically

Unit 6 – !ception "an#ling

Page 26: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 26/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 !ception "an#ling

*inally Bloc4§   finally  )loc4 is e!ec(te# irrespective o* whether the try )loc4

throws an error or not.§ finally  )loc4 is g(arantee# to )e e!ec(te# an# can )e (se# *or any

clean-(p co#e.§ %* no catch )loc4 matches an e!ception thrown< control is trans*erre#

to the finally  )loc4.§ There can )e only one finally  )loc4 *or a try )loc4.

try

E

catch$ExceptionType name&

E

finall$

E

Unit 6 – !ception "an#ling

Page 27: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 27/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Unit 6 !ception "an#ling

*inally Bloc4 !ample*u#lic int count2hars(String fileame) {

  int total = ;

  ileeader r = ne0 ileeader(fileame);

  tr$ {  0hile( r,read$()) {

  r,read();

  total??;

  }

  }

  catch(ileotoundce*tion e){  S$stem,out,*rintln("ile named " ? fileame ? "not

found, " ? e);

  total = -1;

  }

  catch(.ce*tion e){

  S$stem,out,*rintln(".ce*tion occured "? "0hile

counting " ? e);

  total = -1;

  }

  finall$ {

  r,close();  }

Clean (p co#egoes into *inally

)loc4

Unit 6 – !ception "an#ling

Page 28: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 28/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

p g

System-Defined Exception

Unit 6 – !ception "an#ling

Page 29: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 29/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

p g

System-Defined Exception§ IndexOutOfBoundsException :

§ When beyond the bound of index in the object which use index, suchas array, string, and vector

§ ArrayStoreException :§ When assign object of incorrect type to ee!ent of array

§ "egativeArraySi#eException :§ When using a negative si#e of array

§ "u$ointerException : § When refer to object as a nu pointer

§ SecurityException :§ When vioate security% &aused by security !anager

§ Iega'onitorStateException :§ When the thread which is not owner of !onitor invoves wait or notify

!ethod

Unit 6 – !ception "an#ling

Page 30: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 30/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

p g

Throwing an !ception§  ll metho#s (se the throw  statement to throw an e!ception.§ The throw  statement re?(ires a throwa)le o)ect as arg(ment.

throwa)le o)ect are instances o* any s()class o* the Throwable class.

§ throw  ca(ses the metho# to terminate an# ret(rns an e!ceptiono)ect to the caller.

*u#lic static dou#le divide(int a int #)

{dou#le 5uotient=;

  tr${ if (#==)

  { thro0 ne0 7rithmeticce*tion();

  }

  5uotient=a6#;

}

catch (7rithmeticce*tion e)

{ S$stem,out,*rintln(e,getAessage()?"Bn ivisor is 8ero");

}

 return 5uotient;

}

.9ee listing > /hro0ivisionrroemo,ava

Unit 6 – !ception "an#ling

Page 31: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 31/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

Propagation o* (nca(ght e!ception.

Unit 6 – !ception "an#ling

Page 32: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 32/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

§   metho# can speci*y to throw an e!ception )ya##ing a throws cla(se to the metho# #eclaration.

§ The throws cla(se comprises the throws  4eywor#*ollowe# )y a comma-separate# list o* all thee!ceptions thrown )y that metho#.

§ The cla(se goes a*ter the metho# name an#arg(ment list an# )e*ore the )race that #e*ines thescope o* the metho#.

p()lic voi# write'ist$& thro0s ileotoundce*tion

.ce*tion

DDDDDDD

E

The /throws cla(se

Unit 6 – !ception "an#ling

Page 33: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 33/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

p()lic class Propagating!ceptionTest

  voi# metho#:$& throws %!ception

  D..

metho#2$&G

  DD

voi# metho#2$& throws %!ception

DD

  throw new %!ception$&G  DD.

  E

 

p()lic static voi# main$9tringNO args& throws %!ception

Propagation o* (nca(ght e!ception.

Unit 6 – !ception "an#ling

Page 34: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 34/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

!ception han#ling mechanism

+ata)ase

B(siness 'ogic

Control

U%

retrieveUser%n*o$& throws !ception

  retrieves+ata$&

 

  E

E

(ser'oginorm$&

 

  a(thenticateUser%nControl$&GE

Throws

e!ception

a(thenticateUser$& throws !ception

  retrieveUser%n*o$&

E

a(thenticateUser%nControl$&

  a(thenticateUser$&G

E

"an#les

!ception with

try1catch

9ee listing > /hro0semo,ava

Unit 6 – !ception "an#ling

Page 35: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 35/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

9ome Use*(l ,etho#s§ The Throwa)le class has some (se*(l metho#s that can

)e calle# on any !ception type§ 9tring get,essage$&

§ 7et(rns the message that was set while the e!ception o)ect was

create#< )y calling the parameteri@e# constr(ctor ce*tion(String message)

§ 9tring to9tring§ 7et(rns a 9tring representation o* the e!ception o)ect

§ voi# print9tac4Trace$&

Unit 6 – !ception "an#ling

Page 36: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 36/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

9tac4Trace

• *rintStac3/race() metho# helps to trace the origin o* the

!ception

Unit 6 – !ception "an#ling

Page 37: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 37/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

User #e*ine# !ceptions§ %* an e!ception cannot )e

represente# )y those in

the Java plat*orm< a (sercan #e*ine his own

e!ception.

§ The (ser #e*ine#e!ception class sho(l#

)e a s()class o*

!ception or any o* its

s() types.

*u#lic class

ProductNotFoundException 

etends ce*tion

E

p()lic Pro#(ct getPro#(ct$int

pro#%#& throws

ProductNotFoundException

  DDD

  DDD

E

Unit 6 – !ception "an#ling

/

Page 38: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 38/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

/Throwing !ceptions§ Use the /throw cla(se to throw the (ser #e*ine# e!ception o)ect

§ The throw  statement re?(ires a throwa)le o)ect as arg(ment.Throwa)le o)ects are instances o* any s()class o* the Throwable class.

§ throw  ca(ses the metho# to terminate an# ret(rns an e!ceptiono)ect to the caller.

p()lic Pro#(ct getPro#(ct$int pro#%#& throwsPro#(ctAoto(n#!ception

  11 7etrieve pro#(ct in*o *rom the #ata)ase

  11 select Q *rom Pro#(ct where pro#Ri# F pro#%#

  i*$L*o(n#&

  thro0 ne0

>roductotoundce*tion();

  ret(rn pro#(ctG

E

Unit 6 – !ception "an#ling

U # *i # ti

Page 39: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 39/40

Basic JavaCopyright © 2008 Pratian Technologies

www.pratian.com

User #e*ine# !ceptions

!ception

 ge!ception

(t*ge'imit!ception %llegalgeormat!ception

TooSo(ng!ception Tool#!ception Aegativege!ception

9ee listing > 4serefinedce*tionsemo,ava

Unit 6 – !ception "an#ling

ti ti

Page 40: Unit 6 - Exception Handling

7/23/2019 Unit 6 - Exception Handling

http://slidepdf.com/reader/full/unit-6-exception-handling 40/40

Please try to limit the questions to the topics discussed during the session. Thank you.

(estion time