Solved

Catch Error and Rethrow with sensible message

Posted on 2007-11-18
8
572 Views
Last Modified: 2008-02-01
SQL Server 2005 / C#.NET
Hi I have a stored procedure in SQL Server 2005 with Error Handling... Lets say user passes CustomerID Number 2 to be deleted. Then I will get a Foreign key violation in RaiseError. Now i want to Catch that Error message Format it and send back to my client from my Class...  How can I format the Original message being thrown, Modify it and rethrow it back again.

Create Procedure Delete_Customer
@Cust_ID int
As
BEGIN TRY
Delete from Customer where customer_ID =@Cust_ID
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
SELECT @ErrorMessage = ERROR_MESSAGE();
RAISERROR (@ErrorMessage, 16, 1);
END CATCH
0
Comment
Question by:dotnet0824
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20309558
Hi there, try this

            try
            {
                int x;
                int y = 0;
                x = 1 / y;
            }
            catch (DivideByZeroException ex)
            {
                throw new DivideByZeroException("My custom message " + ex.Message);
            }

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 20309597
Whatz that.. did u read my question
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20309685
yes I did read your question, you want to catch an error and rethrow it with a custom message, if you read my code, you will see that is what is happening, you can ignore the divide by zero, I was just using that so that you could see that it is working
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 7

Expert Comment

by:multithreading
ID: 20309806
Given that you are on Sql 2005, you could use .Net for your procedure. Then you could have complete control over the try/catch. It would be more work than the "i wish it were like this" psuedo code you wrote, but after registering the DLL in SQL 2005 it would get you were you are trying to go.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20309922
Create Procedure Delete_Customer
@Cust_ID int
As
BEGIN TRY
Delete from Customer where customer_ID =@Cust_ID
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000)

SELECT @ErrorMessage =
'There was a problem trying to delete customer ['
+ convert(varchar, @Cust_ID) + ']' + Char(13) + Char(10) +
'The error was: ' + Char(13) + Char(10) +
ERROR_MESSAGE()

RAISERROR (@ErrorMessage, 16, 1)
END CATCH
0
 

Author Comment

by:dotnet0824
ID: 20310890
can we use Varchar(MAX) instead of DECLARE @ErrorMessage NVARCHAR(4000)
0
 

Author Comment

by:dotnet0824
ID: 20310898
RedKelvin:yes I did read your question, you want to catch an error and rethrow it with a custom message, if you read my code, you will see that is what is happening, you can ignore the divide by zero, I was just using that so that you could see that it is working

You were saying about DivideByZero exception...being caught @ client. But in my case how can I use the same technique @ the client.....
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20311124
yes varchar(max) will be fine for most standard installations. although i can't imagine you wanting to stuff more than 4000 bytes as an error message.  nvarchar is good for multi-byte system, but i don't think that applies to you
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question