Write PL/SQL block that asks user to input first number, second number and an arithmetic operator (+, -, *, or /). If operator is invalid, throw and handle a user-defined exception. If second number is 0 and the operator is /, handle ZERO_DIVIDE predefined server exception.
What will be an ideal response?
```
SQL> DECLARE
2 bad_op EXCEPTION;
3 n1 NUMBER := &number1;
4 n2 NUMBER := &number2;
5 op VARCHAR2(1) := '&operator';
6 res NUMBER;
7 BEGIN
8 IF op<>'+' AND op<>'-' AND op <>'*' AND op<>'/' THEN
9 RAISE bad_op;
10 END IF;
11 IF op = '+' THEN
12 res := n1 + n2;
13 ELSIF op = '-' THEN
14 res := n1 - n2;
15 ELSIF op = '*' THEN
16 res := n1 * n2;
17 ELSIF op = '/' THEN
18 res := n1 / n2;
19 END IF;
20 DBMS_OUTPUT.PUT_LINE(n1 || op || n2 || '=' || res);
21 EXCEPTION
22 WHEN bad_op THEN
23 DBMS_OUTPUT.PUT_LINE('No such math operator: ' || op);
24 WHEN ZERO_DIVIDE THEN
25 DBMS_OUTPUT.PUT_LINE('Cannot divide by zero');
26 END;
27 /
Enter value for number1: 3
Enter value for number2: 5
Enter value for operator: +
3+5=8
PL/SQL procedure successfully completed.
SQL> /
Enter value for number1: 3
Enter value for number2: 5
Enter value for operator: ?
No such math operator: ?
PL/SQL procedure successfully completed.
SQL> /
Enter value for number1: 3
Enter value for number2: 5
Enter value for operator: /
3/5=.6
PL/SQL procedure successfully completed.
SQL> /
Enter value for number1: 3
Enter value for number2: 0
Enter value for operator: /
Cannot divide by zero
PL/SQL procedure successfully completed.
```
You might also like to view...
Which three steps deploy a virtual machine from a VM Template in the Content Library? (Choose three)
a. Right-click a VM Template and select New VM from This Template b. In the vSphere Web Client navigator, select vCenter Inventory Lists> VM templates in Folders c. Using vSphere Host client, select vCenter Inventory Lists> Content Libraries d. in the vSphere Web Client navigator, select vCenter Inventory Lists> Content Libraries e. Select a Content Library, click Related Objects tab and click Templates
All trees are hierarchical in nature.
What will be an ideal response?