我在Oracle中运行一个代码,要求用户输入路由类型,然后程序根据用户输入路由以及提取的行数获取最长和最短路径。我需要在代码中添加任何异常,我试着想一想在这种情况下我可以添加什么样的异常。我想添加一个异常,如果提取的行是< 1则会出错。但是,我不确定是否存在此类异常。我还需要为无效的路由类型添加例外。我将此表示为IF声明。不确定这是否属于例外情况。
我的代码是:
SET SERVEROUTPUT ON;
SET VERIFY OFF
DECLARE
v_type VARCHAR2 (20);
min_length NUMBER;
max_length NUMBER;
v_count NUMBER;
BEGIN
v_type := '&InsertTypeRoute';
IF v_type = 'Multi-Lane Divided' OR
v_type = 'Paved Divided' OR
v_type = 'Paved Undivided' THEN
SELECT MIN(LENGTH_KM), MAX(LENGTH_KM), COUNT(LENGTH_KM) INTO min_length, max_length, v_count
FROM TBLROUTE WHERE TYPE = v_type;
DBMS_OUTPUT.PUT_LINE('The minimum length is: ' || TO_CHAR(min_length));
DBMS_OUTPUT.PUT_LINE('The maximum length is: ' || TO_CHAR(max_length));
DBMS_OUTPUT.PUT_LINE ('Total number of '|| v_type ||' route is: ' || TO_CHAR(v_count));
ELSE
DBMS_OUTPUT.PUT_LINE ('Route type cannot be found');
END IF;
/*
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Route type cannot be found'); */
END;
/
这些例外怎么样?它们在这种情况下是否合适?
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Data not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('This program encountered an error');
答案 0 :(得分:0)
您可以使用RAISE_APPLICATION_ERROR过程在用户定义的范围(-20000 ..- 20999)中引发特定异常,然后定义异常以便捕获它。以下内容应该有效:
DECLARE
SOMETHING_UNWANTED_HAPPENS BOOLEAN := TRUE;
BEGIN
IF SOMETHING_UNWANTED_HAPPENS THEN
RAISE_APPLICATION_ERROR(-20001, 'Something I didn''t want happened');
END IF;
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -20001 THEN
DBMS_OUTPUT.PUT_LINE('My user-defined exception was raised');
-- Do whatever else you want to do to handle this exception
ELSE
DBMS_OUTPUT.PUT_LINE('Something else happened');
END IF;
END;
或者你可以声明一个EXCEPTION变量,用PRAGMA EXCEPTION_INIT初始化它,然后像任何其他异常一样使用它:
DECLARE
my_exception EXCEPTION;
PRAGMA EXCEPTION_INIT(my_exception, -20001);
SOMETHING_UNWANTED_HAPPENS BOOLEAN := TRUE;
BEGIN
IF SOMETHING_UNWANTED_HAPPENS THEN
RAISE my_exception;
END IF;
EXCEPTION
WHEN my_exception THEN
DBMS_OUTPUT.PUT_LINE('My user-defined exception was raised');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Something else happened');
END;
答案 1 :(得分:0)
“在这种情况下它们是否合适?”
处理这样的异常被普遍认为是不好的做法。它吞下了真正的异常,因此用户知道某些事情失败了,但他们绝对不知道是什么。
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('This program encountered an error');
这是一种无所事事的情况。
在你的情况下,我建议你依靠默认的PL / SQL NO_DATA_FOUND
异常来处理没有记录,并在只有一个时提出一个定制的异常。像这样:
DECLARE
v_type VARCHAR2 (20);
min_length NUMBER;
max_length NUMBER;
v_count NUMBER;
-- user-defined exception
x_single_route exception;
pragme expcetion_init(x_single_route, -20001);
BEGIN
v_type := '&InsertTypeRoute';
IF v_type = 'Multi-Lane Divided' OR
v_type = 'Paved Divided' OR
v_type = 'Paved Undivided'
THEN
SELECT MIN(LENGTH_KM), MAX(LENGTH_KM), COUNT(LENGTH_KM) INTO min_length, max_length, v_count
FROM TBLROUTE WHERE TYPE = v_type;
IF v_count > 1 THEN
DBMS_OUTPUT.PUT_LINE('The minimum length is: ' || TO_CHAR(min_length));
DBMS_OUTPUT.PUT_LINE('The maximum length is: ' || TO_CHAR(max_length));
DBMS_OUTPUT.PUT_LINE ('Total number of '|| v_type ||' route is: ' || TO_CHAR(v_count));
ELSE
raise x_single_route;
END IF;
EXCEPTION
WHEN x_single_route THEN
DBMS_OUTPUT.PUT_LINE ('Route has only one leg');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Route type cannot be found');
END;
/
一般来说,提升异常是一种好习惯:用户想知道什么时候出错。 (虽然这似乎是一个面向用户的程序,所以它略有不同。)
...
EXCEPTION
WHEN x_single_route THEN
DBMS_OUTPUT.PUT_LINE ('Route has only one leg');
RAISE;
WHEN NO_DATA_FOUND THEN
....
答案 2 :(得分:0)
如果您使用 WHEN_OTHERS 以及 RAISE ,建议添加 SQLCODE 和 SQLERRM 这将告诉你究竟出了什么问题。
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR MESSAGE :' || SQLCODE || ' :' || SQLERRM );
RAISE;