plsql
उपवाद सम्भालना
खोज…
परिचय
ओरेकल विभिन्न प्रकार के अपवाद पैदा करता है। आप आश्चर्यचकित हो सकते हैं कि कुछ अस्पष्ट संदेश के साथ आपका कोड रोकना कितना कठिन हो सकता है। अपने पीएल / एसक्यूएल कोड को आसानी से ठीक करने की क्षमता में सुधार करने के लिए सबसे निचले स्तर पर अपवादों को संभालना आवश्यक है। कभी भी एक अपवाद को "कालीन के नीचे" न छिपाएं, जब तक कि आप यहां केवल अपने लिए और किसी और को बनाए रखने के लिए अपने कोड का टुकड़ा नहीं रखते हैं।
हैंडलिंग त्रुटि त्रुटि अपवाद
प्रत्येक मानक ओरेकल त्रुटि एक त्रुटि संख्या के साथ जुड़ा हुआ है। यह अनुमान लगाने के लिए महत्वपूर्ण है कि आपके कोड में क्या गलत हो सकता है। किसी अन्य डेटाबेस के कनेक्शन के लिए यह हो सकता है:
-
-28000
खाता बंद है -
-28001
पासवर्ड समाप्त हो गया -
-28002
अनुग्रह अवधि -
-1017
गलत उपयोगकर्ता / पासवर्ड
डेटाबेस लिंक द्वारा उपयोग किए गए उपयोगकर्ता के साथ क्या गलत है, यह जांचने का एक तरीका है:
declare
v_dummy number;
begin
-- testing db link
execute immediate 'select COUNT(1) from dba_users@pass.world' into v_dummy ;
-- if we get here, exception wasn't raised: display COUNT's result
dbms_output.put_line(v_dummy||' users on PASS db');
EXCEPTION
-- exception can be referred by their name in the predefined Oracle's list
When LOGIN_DENIED
then
dbms_output.put_line('ORA-1017 / USERNAME OR PASSWORD INVALID, TRY AGAIN');
When Others
then
-- or referred by their number: stored automatically in reserved variable SQLCODE
If SQLCODE = '-2019'
Then
dbms_output.put_line('ORA-2019 / Invalid db_link name');
Elsif SQLCODE = '-1035'
Then
dbms_output.put_line('ORA-1035 / DATABASE IS ON RESTRICTED SESSION, CONTACT YOUR DBA');
Elsif SQLCODE = '-28000'
Then
dbms_output.put_line('ORA-28000 / ACCOUNT IS LOCKED. CONTACT YOUR DBA');
Elsif SQLCODE = '-28001'
Then
dbms_output.put_line('ORA-28001 / PASSWORD EXPIRED. CONTACT YOUR DBA FOR CHANGE');
Elsif SQLCODE = '-28002'
Then
dbms_output.put_line('ORA-28002 / PASSWORD IS EXPIRED, CHANGED IT');
Else
-- and if it's not one of the exception you expected
dbms_output.put_line('Exception not specifically handled');
dbms_output.put_line('Oracle Said'||SQLCODE||':'||SQLERRM);
End if;
END;
/
कस्टम अपवाद को परिभाषित करें, इसे बढ़ाएं और देखें कि यह कहां से आता है
इसे समझने के लिए, यहां एक फ़ंक्शन है जिसमें 3 अलग-अलग "गलत" व्यवहार हैं
- पैरामीटर पूरी तरह से बेवकूफ है: हम एक उपयोगकर्ता-परिभाषित अभिव्यक्ति का उपयोग करते हैं
- पैरामीटर में एक टाइपो है: हम Oracle मानक
NO_DATA_FOUND
त्रुटि का उपयोग करते हैं - एक और, लेकिन मामला नहीं संभाला
इसे अपने मानकों के अनुकूल बनाने के लिए स्वतंत्र महसूस करें:
DECLARE
this_is_not_acceptable EXCEPTION;
PRAGMA EXCEPTION_INIT(this_is_not_acceptable, -20077);
g_err varchar2 (200) := 'to-be-defined';
w_schema all_tables.OWNER%Type;
PROCEDURE get_schema( p_table in Varchar2, p_schema out Varchar2)
Is
w_err varchar2 (200) := 'to-be-defined';
BEGIN
w_err := 'get_schema-step-1:';
If (p_table = 'Delivery-Manager-Is-Silly') Then
raise this_is_not_acceptable;
end if;
w_err := 'get_schema-step-2:';
Select owner Into p_schema
From all_tables
where table_name like(p_table||'%');
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- handle Oracle-defined exception
dbms_output.put_line('[WARN]'||w_err||'This can happen. Check the table name you entered.');
WHEN this_is_not_acceptable THEN
-- handle your custom error
dbms_output.put_line('[WARN]'||w_err||'Please don''t make fun of the delivery manager.');
When others then
dbms_output.put_line('[ERR]'||w_err||'unhandled exception:'||sqlerrm);
raise;
END Get_schema;
BEGIN
g_err := 'Global; first call:';
get_schema('Delivery-Manager-Is-Silly', w_schema);
g_err := 'Global; second call:';
get_schema('AAA', w_schema);
g_err := 'Global; third call:';
get_schema('', w_schema);
g_err := 'Global; 4th call:';
get_schema('Can''t reach this point due to previous error.', w_schema);
EXCEPTION
When others then
dbms_output.put_line('[ERR]'||g_err||'unhandled exception:'||sqlerrm);
-- you may raise this again to the caller if error log isn't enough.
-- raise;
END;
/
एक नियमित डेटाबेस पर देना:
[WARN]get_schema-step-1:Please don't make fun of the delivery manager.
[WARN]get_schema-step-2:This can happen. Check the table name you entered.
[ERR]get_schema-step-2:unhandled exception:ORA-01422: exact fetch returns more than requested number of rows
[ERR]Global; third call:unhandled exception:ORA-01422: exact fetch returns more than requested number of rows
याद रखें कि दुर्लभ मामलों को संभालने के लिए अपवाद यहां हैं। मैंने उन अनुप्रयोगों को देखा जिन्होंने हर एक्सेस पर एक अपवाद उठाया, बस उपयोगकर्ता पासवर्ड पूछने के लिए, "कनेक्टेड नहीं" ... इतना अभिकलन बेकार।