Error Handling and Exception handling in SQL Server:-
- An exception means “ Run time error”
- In SQL server if any statement causes run time error then SQL Server returns “error msg” and program execution is continued.
- When any error occurs to display user friendly message and to execute error processing statements and the error must be handled.
- To handle errors we need to provide a block called “try-catch block”.
Create procedure <name> (parameter)
Statements → it is causes error
Statements → it is handle error
if any statement in try block causes exception then control is transfer to catch block and statements in the catch block are executed.
Create procedure div (@x int ,@y int)
Declare @z int
Print @ z
Print ‘ divide by zero error’
Exec div 10,5 Exec div 10,0
Output → 2 Output → divide by zero error.