Compile-time warnings can greatly improve the maintainability of your code and reduce the chance that bugs will creep into it. Compile-time warnings differ from compile-time errors; with warnings, your program will still compile and run. You may, however, encounter unexpected behavior or reduced performance as a result of running code that is flagged with warnings.
This section explores how compile-time warnings work and which issues are currently detected. Let’s start with a quick example of applying compile-time warnings in your session.
A Quick Example
A very useful compile-time warning is PLW-06002: Unreachable code. Consider the following program (available in the cantgothere.sql file on the book’s web site). Because I have initialized the salary variable to 10,000, the conditional statement will always send me to line 9. Line 7 will never be executed.
/* File on web: cantgothere.sql */ 1 PROCEDURE cant_go_there 2 AS 3 l_salary NUMBER := 10000; 4 BEGIN 5 IF l_salary > 20000 6 THEN 7 DBMS_OUTPUT.put_line ('Executive'); 8 ELSE 9 DBMS_OUTPUT.put_line ('Rest of Us'); 10 END IF; 11 END cant_go_there;
If I compile this code in any release prior to Oracle Database 10g, I am simply told “Procedure created.” If, however, I have enabled compile-time warnings in my session on the new release and then try to compile the procedure, I get this response from the compiler:
SP2-0804: Procedure created with compilation warnings SQL>
SHOW errErrors for PROCEDURE CANT_GO_THERE: ...
Get Oracle PL/SQL Programming, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.