SlideShare a Scribd company logo
EXCEPTIONS: 
1. In programs 3 types of error will occur. They are compilation errors, logical errors and runtime 
errors 
2. Compilation errors and logical errors can be rectified or managed by the programmer 
3. Runtime error will manage or rectified by using exception. 
4. Runtime errors are called exceptions.in SQLSERVER 2000 and OLD versions @@ERROR predefined 
global variable is used. 
5. In sqlserver 2005 & in next versions try-catch block will be used. 
SYNTAX: 
BEGIN TRY 
STATEMENT(S) 
END TRY 
BEGIN CATCH 
STATEMENT(S) 
END CATCH 
NOTE: 
1. All the statements will be in try block when runtime occurs try block will through error to the 
catch block 
2. Catch block will rectify the error or it will manage the error so that program can run successfully 
Example 
CREATE PROCEDURE PRCDIV @A INT,@B INT 
AS 
BEGIN 
BEGIN TRY 
DECLARE @C INT 
PRINT @C=@A/@B 
PRINT @C 
END TRY
BEGIN CATCH 
SELECT ERROR_NUMBER(),ERROR_MESSAGE() 
END CATCH 
END 
NOTE: 
ERROR_NUMBER() and ERROR_MESSAGE() are used to display system error number and system 
error message 
CREATE PROCEDURE TO CHECK WHETHER THE GIVEN TABLE IS EXISTING OR NOT 
CREATE PROCEDURE PRC2 
AS 
BEGIN 
BEGIN TRY 
DECLARE @S NVARCHAR(100) 
SELECT @S=’SELECT * FROM EMPLOYEE’ 
EXEC SP_EXECUTE SQL@S 
END TRY 
BEGIN CATCH 
SELECT ERROR_NUMBER() NUMBER 
ERROR_MESSAGE() MESSAGE 
END CATCH 
END

More Related Content

DOC
De so 2
DOCX
PPT
Oracle PL/SQL exception handling
PDF
Pl lab solution
DOCX
21 ijaprr vol1-3-12-17juni
PPTX
Exception handling in SQL with Execution
PPTX
Different Kinds of Exception in DBMS
PPT
04 Handling Exceptions
De so 2
Oracle PL/SQL exception handling
Pl lab solution
21 ijaprr vol1-3-12-17juni
Exception handling in SQL with Execution
Different Kinds of Exception in DBMS
04 Handling Exceptions

Similar to Exceptions in SQL Server (15)

PPTX
Exceptions Triggers function in SQL by Vasant Bhabad
PDF
Handling errors in t sql code (1)
PPT
exception-handling-in-java.ppt
PPT
Lecture 22 - Error Handling
PPT
Error management
PPS
Exception handling in c programming
PPTX
ORACLE PL SQL FOR BEGINNERS
PPTX
Exception Handling in VB.Net
PPTX
Debugging
PPTX
7.error management and exception handling
DOCX
Oracle pl sql
PPT
PPTX
SQL: Error Messages and Error Handling
PPT
Introduction of exception in vb.net
Exceptions Triggers function in SQL by Vasant Bhabad
Handling errors in t sql code (1)
exception-handling-in-java.ppt
Lecture 22 - Error Handling
Error management
Exception handling in c programming
ORACLE PL SQL FOR BEGINNERS
Exception Handling in VB.Net
Debugging
7.error management and exception handling
Oracle pl sql
SQL: Error Messages and Error Handling
Introduction of exception in vb.net
Ad

More from Yaswanth Babu Gummadivelli (20)

PPTX
Presentation on BA
DOCX
E commerce use case documentation.
DOCX
MOM on activity diagram
DOCX
PPTX
Business analyst ppt
PPTX
exception handling
PPTX
PPTX
PDF
Use case for atm
PDF
use case diagramHospital managment system
PDF
Activity diagram for ticket vending machine
DOCX
Extreme programming
DOCX
DOCX
Business Analyst
Presentation on BA
E commerce use case documentation.
MOM on activity diagram
Business analyst ppt
exception handling
Use case for atm
use case diagramHospital managment system
Activity diagram for ticket vending machine
Extreme programming
Business Analyst
Ad

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Lesson notes of climatology university.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Presentation on HIE in infants and its manifestations
Final Presentation General Medicine 03-08-2024.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH

Exceptions in SQL Server

  • 1. EXCEPTIONS: 1. In programs 3 types of error will occur. They are compilation errors, logical errors and runtime errors 2. Compilation errors and logical errors can be rectified or managed by the programmer 3. Runtime error will manage or rectified by using exception. 4. Runtime errors are called exceptions.in SQLSERVER 2000 and OLD versions @@ERROR predefined global variable is used. 5. In sqlserver 2005 & in next versions try-catch block will be used. SYNTAX: BEGIN TRY STATEMENT(S) END TRY BEGIN CATCH STATEMENT(S) END CATCH NOTE: 1. All the statements will be in try block when runtime occurs try block will through error to the catch block 2. Catch block will rectify the error or it will manage the error so that program can run successfully Example CREATE PROCEDURE PRCDIV @A INT,@B INT AS BEGIN BEGIN TRY DECLARE @C INT PRINT @C=@A/@B PRINT @C END TRY
  • 2. BEGIN CATCH SELECT ERROR_NUMBER(),ERROR_MESSAGE() END CATCH END NOTE: ERROR_NUMBER() and ERROR_MESSAGE() are used to display system error number and system error message CREATE PROCEDURE TO CHECK WHETHER THE GIVEN TABLE IS EXISTING OR NOT CREATE PROCEDURE PRC2 AS BEGIN BEGIN TRY DECLARE @S NVARCHAR(100) SELECT @S=’SELECT * FROM EMPLOYEE’ EXEC SP_EXECUTE SQL@S END TRY BEGIN CATCH SELECT ERROR_NUMBER() NUMBER ERROR_MESSAGE() MESSAGE END CATCH END