Ora Exceptii PL SQL

Embed Size (px)

Citation preview

  • 8/12/2019 Ora Exceptii PL SQL

    1/12

    PBD Oracle - Exceptii-1

    Copyright Oracle Corporation, 1998. All rights reserved.

    2323

    Handling ExceptionsHandling Exceptions

    23-2 Copyright Oracle Corporation, 1998. All rights reserved.

    ObjectivesObjectives

    After completing this lesson, you shouldbe able to do the following:

    Define PL/SQL exceptions

    Recognize unhandled exceptions

    List and use different types of PL/SQLexception handlers

    Trap unanticipated errors

    Describe the effect of exceptionpropagation in nested blocks

    Customize PL/SQL exception messages

    After completing this lessonAfter completing this lesson,, you shouldyou should

    be able tobe able to dodo the followingthe following::

    Define PL/SQL exceptions

    Recognize unhandled exceptions

    List and use different types of PL/SQLexception handlers

    Trap unanticipated errors

    Describe the effect of exceptionpropagation in nested blocks

    Customize PL/SQL exception messages

  • 8/12/2019 Ora Exceptii PL SQL

    2/12

    PBD Oracle - Exceptii-2

    23-3 Copyright Oracle Corporation, 1998. All rights reserved.

    Handling Exceptions with PL/SQLHandling Exceptions with PL/SQL What is an exception?

    Identifier in PL/SQL that is raised duringexecution.

    How is it raised?

    An Oracle error occurs.

    You raise it explicitly.

    How do you handle it?

    Trap it with a handler.

    Propagate it to the calling environment.

    What is an exception?

    Identifier in PL/SQL that is raised duringexecution.

    How is it raised?

    An Oracle error occurs.

    You raise it explicitly.

    How do you handle it?

    Trap it with a handler.

    Propagate it to the calling environment.

    23-4 Copyright Oracle Corporation, 1998. All rights reserved.

    Handling ExceptionsHandling Exceptions

    TrapTrap the Exceptionthe Exception

    DECLAREDECLARE

    BEGINBEGIN

    END;END;

    ExceptionExceptionis raisedis raised

    EXCEPTIONEXCEPTION

    ExceptionException

    is trappedis trapped

    PropagatePropagate the Exceptionthe Exception

    DECLAREDECLARE

    BEGINBEGIN

    END;END;

    ExceptionExceptionis raisedis raised

    EXCEPTIONEXCEPTION

    Exception isException is

    not trappednot trapped

    ExceptionException

    propagates to callingpropagates to calling

    environmentenvironment

  • 8/12/2019 Ora Exceptii PL SQL

    3/12

    PBD Oracle - Exceptii-3

    23-5 Copyright Oracle Corporation, 1998. All rights reserved.

    Exception TypesException Types

    Predefined Oracle Server

    Non-predefined Oracle Server

    User-defined

    Predefined Oracle Server

    Non-predefined Oracle Server

    User-defined

    }} ImplicitlyImplicitlyraisedraisedExplicitly raisedExplicitly raised

    23-6 Copyright Oracle Corporation, 1998. All rights reserved.

    Trapping ExceptionsTrapping Exceptions

    EXCEPTION

    WHEN exception1 [OR exception2. . .] THEN

    statement1;

    statement2;

    . . .

    [WHEN exception3[OR exception4 . . .] THEN

    statement1;

    statement2;

    . . .]

    [WHEN OTHERS THEN

    statement1;

    statement2;

    . . .]

    EXCEPTION

    WHEN exception1 [OR exception2. . .] THEN

    statement1;

    statement2;

    . . .

    [WHEN exception3[OR exception4 . . .] THENstatement1;

    statement2;

    . . .]

    [WHEN OTHERS THEN

    statement1;

    statement2;

    . . .]

    SyntaxSyntaxSyntax

  • 8/12/2019 Ora Exceptii PL SQL

    4/12

    PBD Oracle - Exceptii-4

    23-7 Copyright Oracle Corporation, 1998. All rights reserved.

    Trapping Exceptions GuidelinesTrapping Exceptions Guidelines

    WHEN OTHERS is the last clause.

    EXCEPTION keyword starts exception-handling section.

    Several exception handlers are allowed.

    Only one handler is processed before

    leaving the block.

    WHEN OTHERS is the last clause.

    EXCEPTION keyword starts exception-handling section.

    Several exception handlers are allowed.

    Only one handler is processed beforeleaving the block.

    23-8 Copyright Oracle Corporation, 1998. All rights reserved.

    Trapping Predefined Oracle

    Server Errors

    Trapping Predefined Oracle

    Server Errors

    Reference the standard name in theexception-handling routine.

    Sample predefined exceptions:

    NO_DATA_FOUND

    TOO_MANY_ROWS

    INVALID_CURSOR

    ZERO_DIVIDE

    DUP_VAL_ON_INDEX

    Reference the standard name in theexception-handling routine.

    Sample predefined exceptions:

    NO_DATA_FOUND

    TOO_MANY_ROWS

    INVALID_CURSOR

    ZERO_DIVIDE

    DUP_VAL_ON_INDEX

  • 8/12/2019 Ora Exceptii PL SQL

    5/12

    PBD Oracle - Exceptii-5

    23-9 Copyright Oracle Corporation, 1998. All rights reserved.

    23-10 Copyright Oracle Corporation, 1998. All rights reserved.

    Predefined ExceptionPredefined Exception

    BEGIN SELECT ... COMMIT;

    EXCEPTION

    WHEN NO_DATA_FOUND THEN

    statement1;

    statement2;

    WHEN TOO_MANY_ROWS THENstatement1;

    WHEN OTHERS THEN

    statement1;

    statement2;

    statement3;

    END;

    SyntaxSyntaxSyntax

  • 8/12/2019 Ora Exceptii PL SQL

    6/12

    PBD Oracle - Exceptii-6

    23-11 Copyright Oracle Corporation, 1998. All rights reserved.

    Trapping Non-Predefined OracleServer Errors

    Trapping Non-Predefined OracleServer Errors

    DeclareDeclare

    Name theName theexceptionexception

    AssociateAssociate

    CodeCode thethe PRAGMAPRAGMAEXCEPTION_INITEXCEPTION_INIT

    DeclarativeDeclarative SectionSection

    ReferenceReference

    Handle theHandle theraisedraised

    exceptionexception

    ExceptionException--HandlingHandling

    SectionSection

    23-12 Copyright Oracle Corporation, 1998. All rights reserved.

    DECLARE

    e_products_invalidEXCEPTION;

    PRAGMA EXCEPTION_INIT (

    e_products_invalid, -2292);

    v_message VARCHAR2(50);

    BEGIN

    . . .

    EXCEPTION

    WHEN e_products_invalid THEN

    :g_message := 'Product code

    specified is not valid.';

    . . .

    END;

    DECLARE

    e_products_invalidEXCEPTION;

    PRAGMA EXCEPTION_INIT (

    e_products_invalid, -2292);

    v_message VARCHAR2(50);

    BEGIN

    . . .

    EXCEPTION

    WHEN e_products_invalid THEN

    :g_message := 'Product code

    specified is not valid.';. . .

    END;

    Non-Predefined ErrorNon-Predefined ErrorTrap for Oracle Server error number-2292 an integrity constraint violation

    Trap forTrap forOracleOracle ServerServererror numbererror number

    --2292 an2292 an integrity constraint violationintegrity constraint violation

    e_products_invalid EXCEPTION; 1

    PRAGMA EXCEPTION_INIT (

    e_products_invalid, -2292); 2

    e_products_invalid 3

  • 8/12/2019 Ora Exceptii PL SQL

    7/12

    PBD Oracle - Exceptii-7

    23-13 Copyright Oracle Corporation, 1998. All rights reserved.

    23-14 Copyright Oracle Corporation, 1998. All rights reserved.

    Trapping User-Defined

    Exceptions

    Trapping User-Defined

    Exceptions

    Name theName theexceptionexception

    DeclareDeclare

    DeclarativeDeclarative

    SectionSection

    RaiseRaise

    Explicitly raiseExplicitly raisethe exception bythe exception by

    using theusing the RAISERAISE

    statementstatement

    ExecutableExecutable

    SectionSection

    ReferenceReference

    Handle theHandle theraisedraised

    exceptionexception

    ExceptionException--HandlingHandling

    SectionSection

  • 8/12/2019 Ora Exceptii PL SQL

    8/12

    PBD Oracle - Exceptii-8

    23-15 Copyright Oracle Corporation, 1998. All rights reserved.

    User-Defined ExceptionUser-Defined Exception

    [DECLARE]

    e_amount_remaining EXCEPTION;

    . . .BEGIN

    . . .

    RAISE e_amount_remaining;

    . . .

    EXCEPTION

    WHEN e_amount_remaining THEN

    :g_message := 'There is still an amount

    in stock.';. . .

    END;

    [DECLARE]

    e_amount_remaining EXCEPTION;

    . . .

    BEGIN

    . . .

    RAISE e_amount_remaining;

    . . .

    EXCEPTION

    WHEN e_amount_remaining THEN:g_message := 'There is still an amount

    in stock.';. . .

    END;

    ExampleExampleExample

    e_amount_remaining EXCEPTION; 1

    RAISE e_amount_remaining;2

    e_amount_remaining

    3

    23-16 Copyright Oracle Corporation, 1998. All rights reserved.

    Functions for Trapping

    Exceptions

    Functions for Trapping

    Exceptions

    SQLCODE

    Returns the numeric value for the errorcode

    SQLERRM

    Returns the message associated with theerror number

    SQLCODE

    Returns the numeric value for the errorcode

    SQLERRM

    Returns the message associated with theerror number

  • 8/12/2019 Ora Exceptii PL SQL

    9/12

    PBD Oracle - Exceptii-9

    23-17 Copyright Oracle Corporation, 1998. All rights reserved.

    Functions for Trapping ExceptionsFunctions for Trapping Exceptions

    DECLARE

    v_error_code NUMBER;

    v_error_message VARCHAR2(255);

    BEGIN

    ...

    EXCEPTION

    ...

    WHEN OTHERS THEN

    ROLLBACK;

    v_error_code := SQLCODE ;

    v_error_message := SQLERRM ;

    INSERT INTO errors VALUES(v_error_code,

    v_error_message);

    END;

    ExampleExampleExample

    SQLCODE

    SQLERRM

    23-18 Copyright Oracle Corporation, 1998. All rights reserved.

    Calling EnvironmentsCalling EnvironmentsSQL*Plus

    ProcedureBuilder

    Developer/2000Forms

    Precompilerapplication

    An enclosingPL/SQL block

    Displays error number and messageto screen

    Displays error number and messageto screen

    Accesses error number and messagein a trigger by means of theERROR_CODE and ERROR_TEXTpackaged functions

    Accesses exception number throughthe SQLCA data structure

    Traps exception in exception-handling routine of enclosing block

  • 8/12/2019 Ora Exceptii PL SQL

    10/12

    PBD Oracle - Exceptii-10

    23-19 Copyright Oracle Corporation, 1998. All rights reserved.

    Propagating ExceptionsPropagating Exceptions

    BEGINSELECT ...UPDATE ...IF SQL%NOTFOUND THENRAISE e_no_rows;

    END IF;EXCEPTIONWHEN e_integrity THEN ...WHEN e_no_rows THEN ...

    END;

    DECLARE. . .e_no_rows exception;e_integrity exception;PRAGMA EXCEPTION_INIT (e_integrity, -2292);

    BEGINFOR c_record IN emp_cursor LOOP

    END LOOP;EXCEPTIONWHEN NO_DATA_FOUND THEN . . .WHEN TOO_MANY_ROWS THEN . . .END;

    Subblocks can handleSubblocks can handle

    anan exceptionexception ororpasspass

    the exception to thethe exception to the

    enclosing blockenclosing block..

    BEGINSELECT ...UPDATE ...IF SQL%NOTFOUND THENRAISE e_no_rows;

    END IF;EXCEPTIONWHEN e_integrity THEN ...WHEN e_no_rows THEN ...

    END;

    23-20 Copyright Oracle Corporation, 1998. All rights reserved.

    SummarySummary

    Exception types:

    Predefined Oracle Server error

    Non-predefined Oracle Server error

    User-defined error

    Trap exceptions

    Handle exceptions:

    Trap the exception within the PL/SQLblock

    Propagate the exception

    Exception types:

    Predefined Oracle Server error

    Non-predefined Oracle Server error

    User-defined error

    Trap exceptions

    Handle exceptions:

    Trap the exception within the PL/SQLblock

    Propagate the exception

  • 8/12/2019 Ora Exceptii PL SQL

    11/12

    PBD Oracle - Exceptii-11

    23-21 Copyright Oracle Corporation, 1998. All rights reserved.

    Practice OverviewPractice Overview

    Handling named exceptions

    Creating and invoking user-definedexceptions

    Handling named exceptions

    Creating and invoking user-definedexceptions

    23-22 Copyright Oracle Corporation, 1998. All rights reserved.

    Practice OverviewPractice Overview

    Handling named exceptions

    Creating and invoking user-definedexceptions

    Handling named exceptions

    Creating and invoking user-definedexceptions

  • 8/12/2019 Ora Exceptii PL SQL

    12/12

    PBD Oracle - Exceptii-12

    23-23 Copyright Oracle Corporation, 1998. All rights reserved.