Generally, there are two types of exceptions:
1. System-defined Exceptions
System-defined Exceptions
These are pre-defined exceptions situated in Oracle SQL. It is involuntarily raised or executed on encountering a program that violates any Oracle SQL rules.
System-defined exceptions normally are of two types:
1. Named Exceptions These are system defined exceptions which are pre-defined in the Oracle SQL and normally have a name for reference intention. Few of the named exceptions are as follows:
a. ZERO_DIVIDE: This error occurs when a user tries to divide a number by zero.
b. NO_DATA_FOUND: This error occurs when a user fires a query that doesn’t return anything.
c. CURSOR_ALREADY_OPEN: This error occurs when you try to access a Cursor which is already open or which is already being used by any other program.
d. TOO_MANY_ROWS: This error is raised when you try to fetch more than a single row into a variable or a record.
e. LOGIN_DENIED: This error is raised when a user tries to log into the Oracle database with a wrong username or a password.
f. INVALID_CURSOR: This error occurs when you perform an invalid task on a cursor like fetching data from a cursor that is closed.
g. STORAGE_ERROR: This error occurs when PL/SQL database runs out of memory or memory gets malfunctioned.
2. Unnamed Exceptions These are the system defined exceptions that the Oracle provides to its users. These exceptions don’t have a naming system to its block structure. However, such exceptions do have an error code and an error message associated to it.
Such exceptions are handled either by associating the exception code to a name and using it as a named exception or by using the WHEN other THEN exception handler mechanism.
However, Oracle provides us a feature to assign a name to an unnamed System exception which includes the usage of a Pragma which is also known as an Exception_Init. This mechanism is used to link the Oracle system error code to user defined exception name.
Syntax using Exception_Init
|
1
2
3
4
5
6
7
8
9
10
|
DECLARE
Exception_Name Exception;
Pragma
Exception_Init (Exception_Name, Error_Code);
Begin
Execution Section....
Exception
WHEN Exception_Name THEN
Exception Handler....
END;
|
User-defined Exceptions
The user-defined exceptions are the ones that are developed or programmed by the programmer. These are explicitly declared in the declaration section and explicitly raised in the execution section.
A PL/SQL exception definition contains parts such as exception type, error code/ number and an error message for the end-user. Every exception whether it is user-defined or pre-defined (System) has an error code associated with it.
Syntax for User-defined Exceptions
Example
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
declare
admin_id integer;
admin_name varchar2(20);
begin
select ano,aname into admin_id,admin_name from Admin
where ano=admin_id;
dbms_output.put_line(admin_name);
exception
When NO_DATA_FOUND Then
dbms_output.put_line('Administrator Details Unmatched');
When OTHERS Then
dbms_output.put_line('Unknown Error');
End;
/
|
Output
Raise_Application_Error() RAISE_APPLICATION_ERROR() is an in-built procedure which is used to display the user-defined error messages along with an error code which falls in the range of -20000 to -20999. It is only used to raise an exception and there is no method to handle it. Exceptions are raised by the Oracle database server automatically whenever there is any database malfunctioning. However, exceptions can be raised by the programmers by using the RAISE command.
Syntax
|
1
|
Raise_Application_Error (Error_Code, Error_Message);
|
通常,有两种类型的例外:
1.系统定义的异常
系统定义的异常
这些是Oracle SQL中的预定义异常。 它在遇到违反任何Oracle SQL规则的程序时会不由自主地提出或执行。
系统定义的异常通常有两种类型:
1.命名异常这些是系统定义的异常,这些异常是在Oracle SQL中预定义的,通常具有用于参考目的的名称。 很少有命名异常如下:
一个。 ZERO_DIVIDE:当用户尝试将数字除以零时,会发生此错误。
b。 NO_DATA_FOUND:当用户触发不返回任何内容的查询时,将发生此错误。
C。 CURSOR_ALREADY_OPEN:当您尝试访问已打开的游标或任何其他程序正在使用的游标时,会发生此错误。
d。 TOO_MANY_ROWS:当您尝试将多个行读取到变量或记录中时,会引发此错误。
e。 LOGIN_DENIED:当用户尝试使用错误的用户名或密码登录到Oracle数据库时,会引发此错误。
F。 INVALID_CURSOR:当您对游标执行无效任务(例如从关闭的游标中获取数据)时,会发生此错误。
G。 STORAGE_ERROR:当PL / SQL数据库的内存不足或内存出现故障时,将发生此错误。
2.未命名的异常这些是Oracle提供给用户的系统定义的异常。 这些异常的块结构没有命名系统。 但是,此类异常确实具有错误代码和与之相关的错误消息。
通过将异常代码与名称相关联并将其用作命名异常,或通过使用WHEN其他THEN异常处理程序机制,可以处理此类异常。
但是,Oracle提供了一种为未命名的系统异常分配名称的功能,其中包括使用Pragma(也称为Exception_Init)的用法。 此机制用于将Oracle系统错误代码链接到用户定义的异常名称。
使用Exception_Init的语法
用户定义的异常
用户定义的异常是由程序员开发或编程的异常。 它们在声明部分中明确声明,并在执行部分中明确提出。
PL / SQL异常定义包含一些部分,例如异常类型,错误代码/编号和最终用户的错误消息。 无论是用户定义的还是预定义的(系统)异常,都有与之关联的错误代码。
用户定义的异常的语法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Declare
Declaration Section
Begin
Execution Section
Exception
Exception Section …
When ExceptionA Then
ExceptionA - Handling Statements
When ExceptionB Then
ExceptionB - Handling Statements
When ExceptionZ Then
ExceptionZ - Handling Statements
End ;
|
例
输出量
Raise_Application_Error() RAISE_APPLICATION_ERROR()是一个内置过程,用于显示用户定义的错误消息以及错误代码,范围在-20000到-20999之间。 它仅用于引发异常,没有任何方法可以处理它。 每当数据库出现故障时,Oracle数据库服务器都会自动引发异常。 但是,程序员可以使用RAISE命令引发异常。
句法
翻译自: https://www.thecrazyprogrammer.com/2015/07/plsql-exception-handling.html