How is Exception Handling hanled in MySQL?

Questions by StephenRaj   answers by StephenRaj

Showing Answers 1 - 9 of 9 Answers

Saurabh

  • Oct 18th, 2007
 

In MySQL exception handling is done through

Declare {Continue|Exit} handler set <error variable> = <some value>

So whenever an error has occured, the error variable is set a value and then depending upon continue or exit handler, the next statement is executed or the procedure exits.

  Was this answer useful?  Yes

Most programming languages have some type of error handling that allow the programmer to deal with errors or exceptions that are raised within the program. It’s likely you will have at some time encountered errors within SQL statements. It’s these errors that can be handled within the handlers section of a stored procedure. An error, warning or exception is handled using the following syntax.

DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement

The handler type is the action that will happen if the handler is called. This can be either continue to carry on processing the procedure, exit to leave the procedure and rollback (which as yet is unsupported) but likely to be useful when dealing with transactions. Errors can be handled on a number levels, which can be categorized in the following groups. This is defined in the condition_value section.

The different categories are as follows.
1. SQLSTATE
2. SQLWARNING
3. NOT FOUND
4. SQLEXCEPTION
5. mysql_error_code
6. condition_name

Error numbers within MySQL are grouped by SQLSTATE so a handler defined with SQLSTATE will handle all of the conditions raised under a particular SQLSTATE code.

The next levels are SQLWARNING, which groups SQLSTATE codes beginning with 01. NOTFOUND is for errors with an SQLSTATE beginning with 02.

SQLEXCEPTION which is any SQLSTATE outside of these 2 groups.

Mysql_error_code is for individual errors and exceptions.

If you wanted to deal with 2 not found errors in a different way, you could do this using the particular error code in two separate handlers. Condition_name allows us to define our own names for an SQLSTATE or error code.

The final part of the handler is the set condition which is an area used to define code to be performed if the handler is called. It’s likely you would set a variable in this are which could be used later to determine if the handler has been called.

  Was this answer useful?  Yes

emansm

  • Dec 19th, 2014
 

Hi,
Can you tell me the difference between Exception and Error in Mysql
thanx

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions