User Exception
Handling
Mayank A
What is Exception Handling?
Exception handling is a powerful mechanism to manage and respond to runtime errors gracefully, preventing program crashes and
ensuring robust application behaviour. It allows developers to define specific actions when unexpected events occur, maintaining data
integrity and a smooth user experience.
Error Detection Program Stability Controlled Recovery
Identifies issues during execution. Prevents unexpected termination. Defines actions to mitigate impact.
Understanding User-Defined Exception
Handling in PL/SQL
PL/SQL provides the flexibility to create custom exceptions,
allowing developers to tailor error handling to specific business
rules and application logic. This ensures more precise control
over error scenarios that aren't covered by standard Oracle
exceptions.
PL/SQL: Syntax & Example for User-
Defined Exceptions
Declare your custom exception, associate it with an error code if needed, and then raise it within your code when a specific condition is
met. The EXCEPTION block will then catch and handle it.
Syntax: Example Code:
DECLARE DECLARE
exception_name EXCEPTION; x INT := &x; -- Runtime input
PRAGMA EXCEPTION_INIT(exception_name, -20nnn); y INT := &y;
BEGIN div_r FLOAT;
-- Your code logic exp1 EXCEPTION; -- For division by zero exp2
IF condition THEN EXCEPTION;
RAISE exception_name; -- For y > x
END IF; BEGIN IF y = 0 THEN
EXCEPTION RAISE exp1;
WHEN exception_name THEN ELSIF y > x THEN
-- Exception handling logic RAISE exp2;
END; ELSE
div_r := x / y;
DBMS_OUTPUT.PUT_LINE('The result is ' || div_r);
END IF;
EXCEPTION
WHEN exp1 THEN
DBMS_OUTPUT.PUT_LINE('Error: Division by zero is not
allowed');
WHEN exp2 THEN
DBMS_OUTPUT.PUT_LINE('Error: y is greater than x,
please check the input');
END;
Thank You