Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi am ,
when a integer datatype value is inserted into tinyint data type then it will be a outofrange value ,i want to catch that message and want to display on screen

am using store procedure to insert the values ,
only first value is catching the overflow exceptions rest are skiping
can any one help me
Posted
Comments
Sander Rossel 25-Oct-11 10:34am    
Could you be more specific? What database are you using, what language? Do you want to catch the exception on database level or on application level? Why is it possible in the first place that this happens? Isn't user input validated?
pradeep manne 25-Oct-11 10:50am    
Am using sqlserver2000 ,using C# language.i want to catch the exceptions at application level.the INT datatype which is catching is the first datatype in my list,when am trying 3rd column which is tinyint its not catching
Sergey Alexandrovich Kryukov 25-Oct-11 11:22am    
Tag it! C#, platform, UI library you use (if any) or want to use, whatever else which is important...
--SA

1 solution

Pay attention: integer overflow exceptions will not be even thrown be default. To throw them you will need to do the calculation in checked mode, under checked statement. It will throw exception in both cases: wrapping over the MaxValue (0 for unsigned types) boundary down and over the MaxValue boundary up. The type tinyint is unsigned, 0 to 0xFF, which is represented by the type byte in .NET. So, for example:

C#
checked {
    byte b = 200;
    b += 60; //will throw exception, but only in checked mode; without checked mode, it will wrap to become 4
    b = 0;
    b--; //will throw exception, but only in checked mode; without checked mode, it will wrap to become 255
}


In SQL Server, arithmetic overflow will always happen in case of overflow during calculations or converting. See:
http://msdn.microsoft.com/en-us/library/ms163363.aspx[^],
http://msdn.microsoft.com/en-us/library/ms191530.aspx[^],
http://support.microsoft.com/kb/961695[^].

To catch all exceptions, you need to catch them on top of the stack of each thread. UI needs special treatment to catch all exceptions inside main event loop of the application. Both WPF and System.Windows.Forms provide such a mode of handling uncaught UI thread exceptions and event used to handle it withing the loop. See:
http://msdn.microsoft.com/en-us/library/ms157905.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900