如何捕获并重新抛出MySQL中的所有错误 [英] How to catch and re-throw all errors in MySQL

查看:267
本文介绍了如何捕获并重新抛出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.

感谢您的帮助!

编辑-------

此后,我了解到不可能按照我的要求去做:'(.

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;

(错误()不存在)

推荐答案

要捕获所有SQL异常,请使用:

To catch all SQL exceptions, use:

DECLARE EXIT HANDLER FOR SQLEXCEPTION

SQLWARNINGS也可以用来捕获警告.

SQLWARNINGS can be used to catch warnings also.

在异常处理程序中,要引发刚刚捕获的错误或警告,请使用:

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

RESIGNAL

请参阅RESIGNAL语句的文档:

See the documentation for the RESIGNAL statement:

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

此功能自MySQL 5.5起可用

This is available since MySQL 5.5

这篇关于如何捕获并重新抛出MySQL中的所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆