Hi Guest

HomePage

1 1

To improves our performance please Like Share Subscribe(Will boost us)

Handling errors by use of TRY and CATCH in SQL?
Question Posted on 20 Mar 2024

Home >> DataBase >> SQL Query >> Handling errors by use of TRY and CATCH in SQL?

Handling errors by use of TRY and CATCH in SQL?
Below is the syntax to use of TRY and CATCH block. In below example we have two blocks of code:-


TRY CATCH
Anything we write in BEGIN TRY and END Try is the code which we want to monitor for any error. So if an error happend inside of TRY statement, control will immediately get transferred to the CATCH statement and then execute the code line by line of statement in between CATCH block.

Inside the CATCH statement we can fix the error, or we can report the error or evn log error in DB Table. Here in logs we can fectch the username and all useful stuff. And we can even have access to some special data only available inside the CATCH statement:-


  • Error_Number:-This will return the internal number of error.

  • ERROR_STATE:-This will return the information about the source

  • ERROR_SEVERITY:-This will return the information about anything from informational errors to errors user of DBA can fix, etc.

  • ERROR_LINE:-This will returns the line number at which an error happened on.

  • ERROR_PROCEDURE:-This will return the name of the store procedure or function

  • ERROR_MESSAGE:-This will return the most essential information and that is the message text of the error.



When we say about the TRY CATCH block it is quite easy to handle as compare the use of transactions. And the reason of this is you need to take care of COMMIT or ROLLBACK transaction. The problem is if an error occurs after we begin but before we commit or rollback.

And below is the syntax to get the detail of error and create a logs for the error.



Catch Error
Here in above example we have tried for one error in TRY block divide 1 by 0 so that we will get the error and the code jump to CATCH block to capture the error.
0
0



Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions
How to get the table count in last used query from cache by sending tablename?
Write a SQL query to print 1 to 100 in sql server?
Sql Interview Latest Query questions asked on 30 dec 2020
nth highest number from table
some good sql query






Most Visited Questions
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |
     
 
Privacy policy | Terms and Conditions | Blog


If the copyright of any questions or content or syntax belong to you email us we will remove that(info@crackyourinterview.com or crackyourinterview2018@gmail.com)
 

1
Home About Us GroupChat
 
Sitemap Feedback Contact us