how to execute next try on E exception - exception

try
try //try1
//if error happen
except
on E: Exception do
begin
how to execute next try below
end;
end;
try //try2
...
except
on E: Exception do
begin
*and so on*
end;
end;
finally
...
end;
How if I want to execute "try2" when "try1" run to exception?
I have tried to use "Next" but it straight to "finally" in exception

Related

mysql catch all error handling

is there any way to capture all the errors the same way we can do with exceptions e.g. exceptions can be caught like
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
`#failurecode` = RETURNED_SQLSTATE, `#failuremsg` = MESSAGE_TEXT;
SELECT 'FAILURE' AS RESULT, `#failurecode` AS RESULTCODE,
`#failuremsg` AS RESULTMESSAGE;
END;
can all the errors be caught in one statement?

Dec Ada & exception

I have this code in one modules:
PROCEDURE Get (File: IN Ada.Text_IO.File_Type; Item : OUT Rational) IS
N: Integer;
D: Integer;
Dummy: Character;
BEGIN -- Get
LOOP
BEGIN
Ada.Integer_Text_IO.Get(File => File, Item => N);
Ada.Text_IO.Get (File => File, Item => Dummy);
Ada.Integer_Text_IO.Get(File => File, Item => D);
Item := N/D;
if Dummy /= '/' then
........;
end if;
EXIT;
EXCEPTION
when other =>
Ada.Text_IO.Put_Line(" here is exception ");
END;
END LOOP;
END Get;
What is differences with this second code.
The main of my question is if I don't put raise in body of statement of exception what is happen?
PROCEDURE Get (File: IN Ada.Text_IO.File_Type; Item : OUT Rational) IS
N: Integer;
D: Integer;
Dummy: Character;
BEGIN -- Get
LOOP
BEGIN
Ada.Integer_Text_IO.Get(File => File, Item => N);
Ada.Text_IO.Get (File => File, Item => Dummy);
Ada.Integer_Text_IO.Get(File => File, Item => D);
Item := N/D;
if Dummy /= '/' then
........;
end if;
EXIT;
EXCEPTION
when other =>
Ada.Text_IO.Put_Line(" here is exception ");
**raise;**
END;
END LOOP;
END Get;
The main of my question is if I don't put raise in body of statement of exception what is happen???
Thank you very much.
The only difference between both code modules is that the exception (if any is raised during execution of Get) is reraised, i.e. the exception is propagated to the caller of Get.
Whether this is the desired behaviour depends on your needs, i.e. does the caller of Get need to know that an exception occurred?
In your example several kinds of exceptions may occur, e.g.
not reading the correct/expected input (the file to read from does not start with a number)
trying to read from a file that has not been opened
reading D as 0 (thus resulting in a division by 0)
All of these are handled in the same manner by printing "here is exception". The first implementation of Get then silently returns control to the caller (who will not know that anything strange happened). The second implementation, however, will inform the caller by reraising the exception.
For more information see the Ada LRM ยง11.3 (Raise Statements).

mysql error handling with signal/resignal

I would like to have a check for valid load types. If a load type passed into my procedure is invalid, I want to throw an error message via SIGNAL. I also want to have a generic EXIT handler for any other unexpected errors which will rollback any changes. The problem is the generic EXIT handler is taking precedence over my custom SIGNAL and all I get is 'SQLException encountered'. Unless i remove the generic EXIT handler, in which case I get 'Load type is incorrect'.
code:
DECLARE incorrect_load_type CONDITION FOR SQLSTATE '22012';
DECLARE EXIT HANDLER FOR incorrect_load_type
RESIGNAL SET MESSAGE_TEXT = 'Load type is incorrect';
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
begin
SELECT 'SQLException encountered';
rollback;
end;
IF(v_load_type != 'CORRECT TYPE') THEN
SIGNAL incorrect_load_type;
END IF;
Try this
DECLARE incorrect_load_type CONDITION FOR SQLSTATE '22012';
DECLARE EXIT HANDLER FOR incorrect_load_type
begin
SELECT 'Load type is incorrect';
end;
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
begin
SELECT 'SQLException encountered';
rollback;
end;
IF (v_load_type != 'CORRECT TYPE') THEN
SIGNAL incorrect_load_type;
END IF;
What I would do is this:
DECLARE custom_exception CONDITION FOR SQLSTATE '45000';
DECLARE EXIT HANDLER FOR SQLEXCEPTION begin
rollback;
resignal;
end;
IF v_load_type != 'CORRECT TYPE' THEN
SIGNAL custom_exception set message_text = 'Load type is incorrect';
END IF;
Notice that I've created a generic custom exception, and set the message text when it is signaled. This allows the generic SQLEXCEPTION exit handler to catch it and propagate that error message.
Next, I have deleted the handler for the custom exception.
Lastly, I have removed SQLWARNING from the exit handler. This is to avoid catching harmless warnings that ends up aborting the execution (since the "exit" handler would have caught it had SQLWARNING been included) but would yet totally mask out the reason (likely bcoz warnings aren't signaled, I'm guessing. And hence, despite the "resignal", there is nothing to resignal, so nothing is resignaled! And what ends up happening is: A warning (eg. data truncation) is caught and execution aborted, with no indication of this having happened!)
This is what I seem to be seeing from my experience.
Coming from an Oracle background, I can positively state that exception handling in MySQL grossly sucks!

"Cannot use function in a procedure call" compiler error

Recursion towers of Hanoi program in ADA.
So far I think I have most of it down, my problem is being in my solve function.
I think I have the algorithm fine, but I am not sure how to implement it into the function, all examples I see of using this are using the function inside itself such as:
Example
My errors are:
hanoi.adb:23:09: cannot use function "solve" in a procedure call
hanoi.adb:27:09: cannot use function "solve" in a procedure call
hanoi.adb:59:15: missing ")"
Here is my code so far.
with ada.text_io, ada.command_line;
use ada.text_io, ada.command_line;
procedure hanoi is
Argument_Error : EXCEPTION;
max_disks, min_disks : integer := 3;
moves : integer := 0;
verbose_bool : boolean;
function solve (N: in integer; from, to, using: in character) return integer is
begin
if N = 1 then
if verbose_bool = true then
put("Move disk " & integer'image(N) & " from " & character'image(from) & " to " & character'image(to));
end if;
else
solve(N - 1, 'A', 'B', 'C');
if verbose_bool = true then
put("Move disk " & integer'image(N) & " from " & character'image(from) & " to " & character'image(to));
end if;
solve(N - 1, 'B', 'C', 'A');
end if;
moves := (2 ** min_disks) - 1;
return moves;
end solve;
begin
while min_disks /= max_disks loop
IF Argument_Count > 1 THEN
if Argument_Count = 1 then
min_disks := integer'value("Argument(1)");
elsif Argument_Count = 2 then
min_disks := integer'value("Argument(1)");
max_disks := integer'value("Argument(2)");
elsif Argument_Count = 3 then
min_disks := integer'value("Argument(1)");
max_disks := integer'value("Argument(2)");
if argument(3) = "v" or argument(3) = "V" then
verbose_bool := true; -- if argument is V or v it is true
end if;
END IF;
END IF;
IF Argument_Count > 3 THEN
RAISE argument_error;
END IF;
if (max_disks > 0) then
solve (N: integer; from, to, using : character);
END IF;
min_disks := min_disks + 1;
end loop;
EXCEPTION
WHEN Name_Error =>
Put_Line("Please re-enter your arguments, check to see if you entered integers and characters. Max of 3 arguments.");
WHEN OTHERS =>
Put_Line("Please try to not break the program again, thank you.");
end hanoi;
Functions return values, procedures do not, and you've defined Solve as a function.
Ada requires that you do something with a function's returned value, which you're not doing here. (You can't ignore the returned result as is done in other programming languages.)
As the error message states, your syntax is that of making a procedure call, i.e. invoking a procedure, but you've supplied the name of a function.
If the value being returned from a function is meaningful, then act on it in accordance with its purpose. If it is not providing any meaningful functionality, eliminate it and define Solve as a procedure.
As an aside, you may want to re-factor your display code into a nested subprogram. In the outline below, procedure Print can access the parameters of procedure Solve.
procedure Solve (N: in Integer; From, To, Using: in Character) is
procedure Print is
begin
if Verbose then
...
end if;
end Print;
begin
if N = 1 then
Print;
else
Solve (N - 1, 'A', 'B', 'C');
Print;
Solve (N - 1, 'B', 'C', 'A');
end if;
end Solve;
In addition to Marc's comment about the call to Solve's not being a proper Ada function reference, the syntax you have is that of a specification and not that of a invocation of Solve. You had it right in Solve's body just not in the initial invocation:
if (max_disks > 0) then
solve (N: integer; from, to, using : character);
END IF;

Error converting varchar to int?

Msg 245, Level 16, State 1, Line 89
Conversion failed when converting the varchar value 'There was an error creating the System.DirectoryServices assembly. ' to data type int.
This error is caused by the statement below:
begin try print ' - Preparing to create System.DirectoryServices assembly with UNSAFE_ACCESS permission'
create assembly [System.DirectoryServices] from 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll'
with permission_set = unsafe
print ' - System.DirectoryServices assembly created'
end try
begin catch
print 'There was an error creating the System.DirectoryServices assembly. '+Error_number()+Error_Severity()
end catch
This is coming from your concatenation of number to string in the catch block. This is not allowed without casting as it tries to convert the string to number rather than vice versa and as your string literal is not numeric this attempt is doomed to fail!
You can use RAISERROR with severity 0 to print the message with the values substituted in or the alternative is to concatenate the string yourself using explicit casts.
begin catch
declare #num int = Error_number()
declare #sev int = Error_Severity()
raiserror('There was an error creating the System.DirectoryServices assembly. %d %d ',0,1, #num,#sev)
end catch