plsql(轻量版)_异常处理机制
生活随笔
收集整理的這篇文章主要介紹了
plsql(轻量版)_异常处理机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個優秀的程序都應該能夠正確的處理各種出錯異常,并盡可能的從錯誤中恢復,ORACLE提供異常來實現錯誤處理,異常exception這個詞還是這個,處理正常執行過程中未預料的事件,他分為預定義的錯誤,和自定義的錯誤,相當于還可以自定義一個異常,由于PL/SQL塊一旦產生異常,沒有指出如何處理,程序就會自動的終止程序的運行,自動終止程序的運行,就像JAVA里面也是一樣的,他這里有三種異常的錯誤,第一種叫預定義的,預定義的大概有24種異常,然后這個時候它會自動的給你拋出來如果你不使用exception給他捕獲的話,那他就在這里停止了,如果你要捕獲,你就使用exception給他處理一下,這跟JAVA是一樣的,非預定義的異常,就是其他標準的ORACLE錯誤,也是ORACLE指定的一個錯誤,但是他這個錯誤和你自己定義的錯誤,來練習起來,然后自定義一個名,我們來說這兩個怎么來寫,比如我想查詢一個人的工資,如果查工資低于2千,工資太低本來沒有啥問題,你只要這個工資低于多少錢,我就讓你拋一個異常,就是你可以自定義一個異常,然后異常的格式,在大的PL/SQL塊里邊,叫exception,在這里邊寫,如何處理這個異常,當時這個類型的時候,then如何處理,當你有多個異常,when then,when other then
處理異常的方式,預定義的異常大概有24種,異常的情況,有錯誤號,其他錯誤的信息,要想捕獲這個異常,錯誤號它是自動會爆出來的,你把這個內容放到這,比如no_data_found,沒有找到數據,你想更新里面的一個人的工資,結果沒有找到一條數據,他就會拋1403的錯誤,when no_data_found,then 打印一條說,沒有找到數據
他里面有這個類型的,叫to_many_rows,我就查詢一下,declare,定義一個叫v_salary,employees表里面,salary,百分號,type,begin,select salary into v_salary,然后from employees,where,我先寫一個100,就是把100號的工資放到v_salary,dbms打印v_salary,這個程序,我們寫上這個,exception,我們知道這里面可能會出現什么異常,when..then..,這個發現沒問題,100號的工資是24240declarev_salary employees.salary%type;beginselect salary into v_salary from employeeswhere employee_id = 100;dbms_output.put_line(v_salary);end;
這兒這個程序,我們寫上也行,只是我們現在不知道這里面會出現什么異常,我們這里改成一個大于,這顯然不是一個人的員工的查詢結果了,正常我們要使用游標,這是多條記錄,現在這里實際上是多條記錄,實際返回的行數超出請求的行數declarev_salary employees.salary%type;beginselect salary into v_salaryfrom employeeswhere employee_id > 100;end;
too_many_rows這就是他錯誤的名字,錯誤的標號,那我在我相應的exception這里,when,叫to_many_rows,then,輸出的行數太多了,就這樣寫,這不就執行了declarev_salary employees.salary%type;beginselect salary into v_salaryfrom employeeswhere employee_id > 100;dbms_output.put_line(v_salary);exceptionwhen too_many_rows then dbms_output.put_line('輸出的行數太多了!!!!!');end;
輸出的行數太多了,就這樣寫,相當于他拋出來這樣一個對象一樣,然后我們看這個對象對應的類型,是這個類型的你給他輸出,你也可以給他補一下,when others then,出現其他類型的異常的時候,這個就叫預定義的異常,就是他出現異常的錯誤在這里面有標識了declarev_salary employees.salary%type;beginselect salary into v_salaryfrom employeeswhere employee_id > 100;dbms_output.put_line(v_salary);exception when too_many_rows then dbms_output.put_line('輸出的行太多了!!');when others then dbms_output.put_line('出現其他類型的異常了!');end;
非預定義異常的處理,我們需要再PL/SQL塊中聲明一個異常,然后把你出現的錯誤代碼,跟我這個異常的對象,關聯起來,使用這樣一個語句,給我們操作一個
我使用SQL語句寫一個,delete from employees,where employee_id等于100,說違反完整性約束條件
這個刪除知道為什么不能刪嗎,外鍵有一個manager_id,但是manager_id是指向你自己的employee_id,你刪除這個主鍵的時候,外鍵manager_id有人指向他,所以他不讓你刪,這個叫2292,報這個錯,這里面沒有2292,要是有的話你就when處理,他沒有這個錯誤的24種,那你就得這樣寫,declare,那你就得自己定義這個異常,叫delete_id,e_deleteid_exception,exception類型的,光這樣寫還不行,你得使用這個語句,給他關聯起來,pragma,是這樣,然后把你這個名放到這,剛才出現這個錯誤號,2292,-2292,這就寫完了,然后呢,begin,我們剛才想刪除的放到這,delete from,where,100號員工刪除,他一刪除的時候,他就拋這個錯,然后exception,when,當你出現這個異常的時候,違反完整性約束條件,故不可以刪除此用戶,enddeclarev_deleteid_exception exception;pragma exception_init(e_deleteid_exception,-2292);begindelete from employees where employee_id = 100;exceptionwhen e_deleteid_exception then dbms_output.put_line('違反完整性約束條件,故不可以刪除此用戶!');end;
他就不給你報這個錯了,第一種區別呢,我們試圖去查詢一下salary的工資,結果我們發現長度過多了,就因為這個錯誤,發現這個框里有,有的話,我們就可以用預定義的異常,結果這里有一些錯誤,發現他報的號我這個表里找不到,既然這個號又給你爆出來了,說明ORACLE已經定義了這個錯誤,他有這個錯,那為就把這個錯和我定義的異常關聯起來,我就可以定義exception的類型一樣,然后讓這個變量和錯誤的關聯起來,當你一旦執行這個語句,發現發生這個錯誤編號的時候,就自動讓你轉成這個異常的變量,然后再給他輸出出來,這就叫非預定義異常,有預定義的異常,有非預定義的異常,還有個叫用戶自定義的異常,用戶自定義的,用戶定義的異常錯誤是顯示用RAISE語句來觸發的,當引發異常錯誤的時候,控制就會轉向到EXCEPTION塊異常錯誤的部分,執行錯誤的代碼,看看這個
我們這樣,還是查詢100號員工的工資,如果他的工資大于1萬,我就拋一個異常說,工資太高了,declare,還得聲明一個異常的變量,e_too_high_sal,exception類型的,他得定義一個變量,整體要查的員工的工資,然后begin,select salary into v_sal,from employees這個表,where employee_id等于100,把100號員工的工資查出來,如果v_sal查出來大于1萬的,then就處理,那怎么處理,就拋這個異常,如果你要是小于1萬的,我就不給你處理了,我就直接end if結束,沒了,end,忘了exception了,你拋出來了你還沒有處理呢,when,當你發生這個異常的時候,then,dbms_output.put_line,打印,相當于你這個異常到底是干什么的,工資太高了,就這樣一個,declaree_too_high_sal_exception;v_sal employees.salary%type;beginselect salary into v_sal from employees where employee_id = 100;if v_sal > 10000 thenraise e_too_high_sal;end if;exceptionwhen e_too_high_sal then dbms_output.put_line('工資太高了!');end;
相當于你這個異常是干什么的,工資太高了,這樣一個,這個就叫用戶自定義的異常,我們剛才定義的三個其實是分開寫的,你也可以給合起來,這個沒問題,就是整個放在一起的話,我們有一個刪除的,刪除的在這,就是剛才這個,delete的錯誤,delete里面我定義了這兩個,非預定義的異常,然后執行一個delete,你們就看誰先執行,誰后執行了,放在這的話detele先執行,delete如果沒錯的話,那就刪了,有錯的話,他就直接跑到這兒來了,你也可以再加上一個when,others then,發生其他的異常了,然后給他end,執行的話只要你執行出現異常了,我就exception我就去找,后邊的代碼就不再執行了,后邊就會拋這個異常
這里就相當于子程序來執行,只要你執行到哪一塊出現異常了,我就去exception里面去找,后邊的代碼就不再執行了,所以就會拋這個異常,因為你這一塊代碼不會被執行,就這個意思,這是說的異常,異常后邊還有一些練習,這三種異常的形式,18. 異常的基本程序:
通過 select ... into ... 查詢某人的工資, 若沒有查詢到, 則輸出 "未找到數據"declare--定義一個變量v_sal employees.salary%type;
begin--使用 select ... into ... 為 v_sal 賦值select salary into v_sal from employees where employee_id = 1000;dbms_output.put_line('salary: ' || v_sal);
exceptionwhen No_data_found then dbms_output.put_line('未找到數據');
end;或declare--定義一個變量v_sal employees.salary%type;
begin--使用 select ... into ... 為 v_sal 賦值select salary into v_sal from employees;dbms_output.put_line('salary: ' || v_sal);
exceptionwhen No_data_found then dbms_output.put_line('未找到數據!');when Too_many_rows then dbms_output.put_line('數據過多!');
end;如若沒找到,就輸出未找到數據,未找到數據,你先這樣看一下,我們寫這個題,你說這個叫預定義,或者非預定義,這個也不好說,那你就先讓他執行一下,他說讓你查詢某個人的工資,看這個人有沒有,我就declare,begin,select salary into,from employees,where,這樣處理的話肯定沒有問題,end他就能夠找到,他找到給他打印一下declarev_sal employees.salary%type;beginselect salary into v_sal from employees where employee_id = 100;dbms_output.put_line(v_sal);end;
這是他的工資,然后你要輸出比如1001,這人不在,說未找到數據,這不就出現一個異常嗎,ORA-1403,你在這找找declarev_sal employees.salary%type;beginselect salary from employees where employee_id = 1001;dbms_output.put_line(v_sal);end;
有的話就是預定義的,沒有的話你得自己去定義一個,1403就是這個吧,有這個就好辦了,就是預定義的,就是定義好的
在這兒,exception,when,no_data_found,then,查無此人,那就這樣子,這個就相當于叫預定義的declarev_sal employees.salary%type;beginselect salary into v_sal from employees where employee_id = 100;dbms_output.put_line(v_sal);exceptionwhen no_data_found then dbms_output.put_line('查無此人');end;
這個相當于是預定義的,還有對應的異常的類型,沒有的話就自己造一個19. 更新指定員工工資,如工資小于300,則加100;對 NO_DATA_FOUND 異常, TOO_MANY_ROWS 進行處理.
declarev_sal employees.salary%type;
beginselect salary into v_sal from employees where employee_id = 100;if(v_sal < 300) then update employees set salary = salary + 100 where employee_id = 100;else dbms_output.put_line('工資大于300');end if;
exceptionwhen no_data_found then dbms_output.put_line('未找到數據');when too_many_rows then dbms_output.put_line('輸出的數據行太多');
end;如果工資小于300,加100,對于沒有找到數據的,還有輸出行太多的,更新指定員工的工資,如果工資小于300,指定員工的,工資小于300,也得先定義一個工資,然后把工資給他放在這里邊,假設101這個人,放到這里面,然后,我判斷一下,這個v_sal他是否是小于300的,如果是小于300的,則加100,then的話加100,salary加100,then相當于做一個更新操作,如果你這個人的工資小于300,我就update,employees,set,salary等于salary加上100,指定員工的,這是對于確實小于100,如果你這個員工可能不在,不在的話就輸出他,還有可能說你寫的這個salary,employee_id,when too_many_rows,then,輸出的行太多了,就這樣處理,101我們看這個,end if,說明就沒有異常declarev_sal employees.salary%type;beginselect salary into v_sal from employees where employee_id = 101;if v_sal<300 then update employees set salary=salary+100 where employee_id = 101;end if;exceptionwhen no_data_found then dbms_output.put_line('查無此人');when too_many_rows then dbms_output.put_line('輸出的行數太多了!');end;
20. 處理非預定義的異常處理: "違反完整約束條件"declare--1. 定義異常 temp_exception exception;--2. 將其定義好的異常情況,與標準的 ORACLE 錯誤聯系起來,使用 EXCEPTION_INIT 語句PRAGMA EXCEPTION_INIT(temp_exception, -2292);
begindelete from employees where employee_id = 100;exception--3. 處理異常when temp_exception thendbms_output.put_line('違反完整性約束!');
end;刪除這個人,包2292這個異常,關聯起來,然后在when這里接收21. 自定義異常: 更新指定員工工資,增加100;若該員工不存在則拋出用戶自定義異常: no_resultdeclare--自定義異常 no_result exception;
beginupdate employees set salary = salary + 100 where employee_id = 1001;--使用隱式游標, 拋出自定義異常if sql%notfound thenraise no_result;end if; exception--處理程序拋出的異常when no_result thendbms_output.put_line('更新失敗');
end;它使用用戶自定義的,正常這個員工不存在的話,直接使用no_data_found就可以了,還有想用戶自定義的也行,update employees set salary = salary + 100,當employee_id是他的時候,它使用的叫隱式游標,因為這個是異常數據,判斷是否在,就raise一個異常,這個對象是我自定義的,我們看一下就行,更新失敗declareno_result exception;beginupdate employees set salary = salary + 100 where employee_id = 100;if sql%notfound thenraise no_result;end if;exceptionwhen no_result thendbms_output.put_line('更新失敗');end;
總結
以上是生活随笔為你收集整理的plsql(轻量版)_异常处理机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: plsql(轻量版)_游标的使用2
- 下一篇: plsql(轻量版)-存储函数存储过程