Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch and re-throw all errors in MySQL

I can't seem to find anywhere how to catch and re-throw any errors or warnings that can occur in a procedure.

What I want is the syntax to do the following:

create procedure myProcedure()   begin        declare exit handler for ANYTHING_WRONG_THAT_CAN_BE_CAUGHT_WARNINGS_INCLUDED       begin           rollback;           RE_THROW THE_THING_THAT_WAS_CAUGHT;       end;        start transaction;          -- do some stuff       commit;   end; // 

The reason being that I want to force a rollback on an error or warning but leave it up to the client to decide what to do with the specific error.

The all-cap areas are the portions where I do not know what to put.

Thanks for any help!

Edit -------

I have since learned it is not possible to do what I have asked :'(.

Instead I have a single error for anything that goes wrong and used the following code:

declare exit handler for sqlwarning, sqlexception begin     rollback;     call error(); end; 

(error() does not exist)

like image 554
nikdeapen Avatar asked Oct 26 '12 23:10

nikdeapen


People also ask

How do I clear MySQL errors?

You can try "\! clear" , it will execute clear shell command. "\!" is used to execute shell command. in *nix it will clear your command prompt.

How would the error message be displayed again in MySQL?

The SHOW COUNT(*) ERRORS statement displays the number of errors. You can also retrieve this number from the error_count variable: SHOW COUNT(*) ERRORS; SELECT @@error_count; SHOW ERRORS and error_count apply only to errors, not warnings or notes.

What is the meaning of the action continue in exception handling in MySQL?

If a condition whose value matches the condition_value , MySQL will execute the statement and continue or exit the current code block based on the action . The action accepts one of the following values: CONTINUE : the execution of the enclosing code block ( BEGIN … END ) continues.


1 Answers

To catch all SQL exceptions, use:

DECLARE EXIT HANDLER FOR SQLEXCEPTION 

SQLWARNINGS can be used to catch warnings also.

Inside the exception handler, to raise the error or warning that was just caught, use:

RESIGNAL 

See the documentation for the RESIGNAL statement:

http://dev.mysql.com/doc/refman/5.5/en/resignal.html

This is available since MySQL 5.5

like image 190
Marc Alff Avatar answered Sep 28 '22 03:09

Marc Alff