Error Management Features

Embed Size (px)

Citation preview

  • 8/12/2019 Error Management Features

    1/34

    Copyright 2000-2006 Steven Feuerstein - Page 1

    OPP 2007February 28 March 1, 2007

    San Mateo Marriott

    San Mateo, California

    An ODTUG SP* Oracle PL/SQL

    Programming Conference

    *SP Seriously PracticalConference

    For more information isit !!!"o#tu$"com or call %10&'(2&7'''

    O)+ -alei#osco.e/une 18 21, 2007

    Pre&conference an#s&on rainin$ & /une 1 17

    ilton )aytona each Oceanfront 3esort)aytona, Flori#a

    WOW-Wide Open World, Wide Open Web

  • 8/12/2019 Error Management Features

    2/34

    Copyright 2000-2006 Steven Feuerstein - Page 2

    Ma4in$ the Most of

    Oracle P56S5

    rror Mana$ement Features

    Steven FeuersteinPL/SQL EvangelistQuest Software

    [email protected]

  • 8/12/2019 Error Management Features

    3/34

    Copyright 2000-2006 Steven Feuerstein - Page 3

    Ten Years Writing Ten Bookson the Oracle PL/SQL Language

  • 8/12/2019 Error Management Features

    4/34

    Copyright 2000-2006 Steven Feuerstein - Page 4

    How to benefit most from this class

    Watch, listen, ask questions.

    Download the training materials and supporting scripts:

    http://oracleplsqlprogramming.com/resources.html

    "Demo zip": all the scripts I run in my classavailable athttp://oracleplsqlprogramming.com/downloads/demo.zip

    Use these materials as an accelerator as you venture intonew territory and need to apply new techniques.

    Play games! Keep your brain resh and active by miing

    filename9from9#emo9:i."s;l

  • 8/12/2019 Error Management Features

    5/34Copyright 2000-2006 Steven Feuerstein - Page 5

    Manage errors effectiel! an" consistentl!

    # signiicant challenge in any programmingenvironment.

    Ideally# errors are raised# handled#logged and communicated in aconsistent# robust manner

    $ome special issues or P%&$'% developers

    $he %&'%($I)* datatype

    +ow to ,ind the line on which theerror is raised-

    'ommunication with non(/S0 hostenvironments

  • 8/12/2019 Error Management Features

    6/34Copyright 2000-2006 Steven Feuerstein - Page 6

    #chieing i"eal error management

    Deine your requirements clearly Understand P%&$'% error management

    eatures and ma"e ull use o what P%&$'%

    has to oer #pply best practices.

    'ompensate ,or (/S0 wea1nesses

    Single point o, de,inition: usereusable components to ensureconsistent# robust error management

  • 8/12/2019 Error Management Features

    7/34Copyright 2000-2006 Steven Feuerstein - Page 7

    $efine !our re%uirements clearl!

    When will errors be raised, when handled( Do you let errors go unhandled to the

    host# trap locally# or trap at the topmost level-

    )ow should errors be raised and handled(

    2ill users do whatever they want or willthere be standard approaches that

    everyone will ,ollow- Useul to conceptuali*e errors into three

    categories:

  • 8/12/2019 Error Management Features

    8/34

    Copyright 2000-2006 Steven Feuerstein - Page 8

    $ifferent t!&es of e'ce&tions

    Deliberate

    $he code architecture itsel, deliberatelyrelies on an e3ception. %3ample:4$5FI%.6%$5I*%

    Unortunate It is an error# but one that is to be

    e3pected and may not even indicate aproblem. %3ample: S%%'$ I*$) 7

    *)5D8$85F)4*D Unepected

    8 "hard" error that indicates a problem

    e

  • 8/12/2019 Error Management Features

    9/34

    Copyright 2000-2006 Steven Feuerstein - Page 9

    PL/SQL error management features

    Deining eceptions

    +aising eceptions

    )anding eceptions ceptions and D-%

  • 8/12/2019 Error Management Features

    10/34

    Copyright 2000-2006 Steven Feuerstein - Page 10

    Qui() Test !our e'ce&tion han"ling know*how

    DECLARE

    aname VARCHAR2(5);

    BEGIN

    BEGIN

    aname := 'Justice';

    DB!"#$%&$%&$%"LINE (aname); ECE&%I#N

    HEN VAL$E"ERR#R

    %HEN

    DB!"#$%&$%&$%"LINE ('Inne* +,-c.');

    END;

    DB!"#$%&$%&$%"LINE ('/at e**-*0');

    ECE&%I#N HEN VAL$E"ERR#R

    %HEN

    DB!"#$%&$%&$%"LINE ('#ute* +,-c.');

    END;

    e

  • 8/12/2019 Error Management Features

    11/34

    Copyright 2000-2006 Steven Feuerstein - Page 11

    $efining +'ce&tions

    he /0P123 is a limited type o data. +as ;ust two attributes: code and

    message.

    9ou can 8IS% and handle ane3ception# but it cannot be passed asan argument in a program.

    4ive names to error numbers with the

    /0P1235131 P+#4-#.

    CREA%E #R RE&LACE &R#CED$RE u1"3-*"e1t (

    e1t"in IN em1,-4eee1a*tment"i%6&E

    7 ne8sa,"in IN em1,-4eesa,a*4%6&E

    )

    I!

    +u,."e**-*s ECE&%I#N;

    &RAGA ECE&%I#N"INI% (+u,."e**-*s7 92

  • 8/12/2019 Error Management Features

    12/34

    Copyright 2000-2006 Steven Feuerstein - Page 12

    ,aising +'ce&tions

    +#1$ raises the speciied eception byname.

    8IS%< reraises current e3ception.'allable only within the e3ception

    section.

    +#1$5#PP%10#1235++2+

    'ommunicates an application speci,ic

    error bac1 to a non(/S0 hostenvironment.

    %rror numbers restricted to the=>#??? =>#>>> range.

  • 8/12/2019 Error Management Features

    13/34

    Copyright 2000-2006 Steven Feuerstein - Page 13

    -sing ,#.S+#PPL.0#T.O1+,,O,

    I> :NE+i*t/ate ? ADD"#N%H! (!6!DA%E7 9 @ < @ 2)

    %HEN

    RAI!E"A&&LICA%I#N"ERR#R

    (927 Em1,-4ee must +e ;

    0ommunicate an error number and message to a

    non6P%&$'% host environment.

    $he ,ollowing code ,rom a databasetriggers shows a typical !and problematicusage o, 8IS%58((I'8$I)*5%):

    RAI!E"A&&LICA%I#N"ERR#R (num +ina*4"intee*7 ms Fa*c/a*27

    .ee1e**-*stac. +--,ean e3au,t >AL!E);

  • 8/12/2019 Error Management Features

    14/34

    Copyright 2000-2006 Steven Feuerstein - Page 14

    Qui(2 #n +'ce&tional Package

    $o 1 create the valerr pac"age and then eecute theollowing command. What is displayed on the screen(

    &ACAGE Fa,e**

    I!

    >$NC%I#N

    et RE%$RN VARCHAR2;

    END Fa,e**;

    !L? EEC$%E 1, (Fa,e**et);

    &ACAGE B#D6 Fa,e**

    I! F VARCHAR2() := a+c;

    >$NC%I#N et RE%$RN VARCHAR2 I!

    BEGIN

    RE%$RN F;

    END;

    BEGIN

    1, ('Be3-*e I s/-8 4-u F');ECE&%I#N

    HEN #%HER! %HEN

    1, (%*a11e t/e e**-*);

    END Fa,e**;

    alerr".4$

    alerr2".4$

  • 8/12/2019 Error Management Features

    15/34

    Copyright 2000-2006 Steven Feuerstein - Page 15

    Han"ling +'ce&tions

    he /0P123 section consolidates all errorhandling logic in a bloc". @ut only traps errors raised in the e3ecutable

    section o, the bloc1.

    $everal useul unctions usually come into play: S0')D% and S0% D@S54$II$9.F)8$5%)5S$8'A

    D@S54$II$9.F)8$5%)5@8'A$8'%

    he D7-$5++%24 pac"age 0uic1 and easy logging o, D errors

    he #8+ $+9+++2+ trigger Instancewide error handling

  • 8/12/2019 Error Management Features

    16/34

    Copyright 2000-2006 Steven Feuerstein - Page 16

    $BMS-T.L.TY error functions

    4et the ull error message withD7-$5U1%1.82+-#5++2+5$#0K

    S0% might truncate the message.

    4se S0% went you want to obtain the

    message associated with an error number. 8ind line number on which error was raised with

    D7-$5U1%1.82+-#5++2+57#0K+#0

    Introduced in )racleB>g elease =# this

    ,unction returns the ,ull stac1 o, errors withline number in,ormation.

    Formerly# this stac1 was available only i, youlet the error go unhandled.

    bac4trace"s;l

  • 8/12/2019 Error Management Features

    17/34

    Copyright 2000-2006 Steven Feuerstein - Page 17

    $BMS+,,LO3 4Oracle56g,78

    #llows D-% statements to eecute againstall rows, even i an error occurs. $he )6 %)S clause speci,ies how

    logging should occur.

    4se the D@S5%)6 pac1age toassociate a log table with Doperations on a base table.

    -uch aster than trapping errors, logging,

    and then continuing&recovering. 3ote: 82+#%% with $#9 /0P123$

    oers similar capabilities.#bms9errlo$"*

  • 8/12/2019 Error Management Features

    18/34

  • 8/12/2019 Error Management Features

    19/34

    Copyright 2000-2006 Steven Feuerstein - Page 19

    +'ce&tions an" $ML

    D-% statements generally are notrolled bac" when aneception is raised. $his gives you more control over your

    transaction.

    +ollbac"s occur with...

    4nhandled e3ception ,rom the outermost(/S0 bloc1#$ND

    %HEN

    e**1.*ec-*"an"c-ntinue(

    !LC#DE7

    ' N- c-m1an4 3-* i ' %#"CHAR (F"i));

    HEN #%HER!

    %HEN

    e**1.*ec-*"an"st-1;

    END;

    he #eelo.er sim.ly

    de!cribe!

    the #esire# action"

    S if i th

  • 8/12/2019 Error Management Features

    27/34

    Copyright 2000-2006 Steven Feuerstein - Page 27

    S&ecif!ing the error

    )ow should 1 speciy the application6speciic error 1 need to raise(

    ; >>> all the time(; Pic" one o those ?>>> numbers rom

    6=>@@@ to 6=>>>>(; Use any positive error number

    besides ? and ?>>(; Use error namesinstead o numbers(

    # i" h " "i f 76 111 +

  • 8/12/2019 Error Management Features

    28/34

    Copyright 2000-2006 Steven Feuerstein - Page 28

    #oi" har"*co"ing of *76

  • 8/12/2019 Error Management Features

    29/34

    , i /h "l b b >

  • 8/12/2019 Error Management Features

    30/34

    Copyright 2000-2006 Steven Feuerstein - Page 30

    ,aise/han"le errors b! number===or name>

    he above trigger ragment illustrates a common

    problem: )ard6coding o error numbers andmessages.

    0ertainly, it is better to use named constants, as in:

    BEGIN

    I> em1,-4ee"*1is"t-"4-un (:ne8/i*e"ate)%HEN

    RAI!E"A&&LICA%I#N"ERR#R (

    9257'6-u must +e at ,east < 4ea*s -,');

    END I>;

    BEGIN

    I> em1,-4ee"*1is"t-"4-un (:ne8/i*e"ate)

    %HEN RAI!E"A&&LICA%I#N"ERR#R (

    em1,-4ee"*1en"t--"4-un

    7 em1,-4ee"*1em"t--"4-un);

    END I>;

    ut no! !e hae acentrali:e#

    #e.en#ency"

    , i i b

  • 8/12/2019 Error Management Features

    31/34

    Copyright 2000-2006 Steven Feuerstein - Page 31

    ,aising errors b! name

    Use an error name Bliteral valueC. $he code compiles now.

    ater# I de,ine that error in the

    repository. *o central point o, ,ailure.

    Downsides: ris" o typos, runtimenotiication o undeined error.

    BEGIN

    I> em1,-4ee"*1is"t-"4-un (:ne8/i*e"ate)%HEN

    M"*untime*aise"e**-* (

    'E&L#6EE9%##96#$NG'

    7 name"in =?'LA!%"NAE'

    7 Fa,ue"in =?:ne8,ast"name);

    END I>;

    n

  • 8/12/2019 Error Management Features

    32/34

    Copyright 2000-2006 Steven Feuerstein - Page 32

    Summar!2 an +'ce&tion Han"ling #rchitecture

    -a"e sure you understand how it all wor"s

    %3ception handling is tric1y stu,,

    $et standards beore you start coding Its not the 1ind o, thing you can easily add in

    later

    Use standard inrastructure components %veryone and all programs need to handle

    errors the same way

    a"e ull advantage o error management eatures.

    S8H% %&'%($I)*S# D@S5%)6#D@S54$II$9.F)8$5%)5@8'A$8'%...

    DonAt accept the limitations o 2racleAs currentimplementation.

    More PL/SQL Please)

  • 8/12/2019 Error Management Features

    33/34

    Copyright 2000-2006 Steven Feuerstein - Page 33

    More PL/SQL< Please)

    7ryn %lewellyn, P%&$'% Product -anager,will hold orth at the Database 4uru %ounge

    at ?=:E> P- today.

    Located in the Database Demorounds area which is in the !est"all at the rear# dead center andunder the area D sign

  • 8/12/2019 Error Management Features

    34/34

    Copyright 2000-2006 Steven Feuerstein - Page 34

    OPP 2007February 28 March 1, 2007San Mateo Marriott

    San Mateo, California

    An ODTUG SP* Oracle PL/SQL

    Programming Conference

    *SP Seriously PracticalConference

    For more information isit !!!"o#tu$"com or call %10&'(2&7'''

    O)+ -alei#osco.e/une 18 21, 2007

    Pre&conference an#s&on rainin$ & /une 1 17

    ilton )aytona each Oceanfront 3esort

    )aytona, Flori#a

    WOW-Wide Open World, Wide Open Web