C# Error Handling Database
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel c# error handling in constructor 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. c# error handling get line number The content you requested has been removed. You’ll be auto redirected in 1 second. Windows Communication Foundation c# error handling framework WCF Feature Details Exception Management in WCF Exception Management in WCF Handling Database Errors Handling Database Errors Handling Database Errors Exception Management Best Practices Implementing Exception Management, Part
C# Error Handling Techniques
1 Implementing Exception Management, Part 2 Handling Database Errors Validation, Part 1 Validation, Part 2 TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Handling Database Errors Visual Studio c# error handling class 2010 Applies to: Windows Communication Foundation Published: June 2011 Author: Alex Culp This topic contains the following sections. Handling Database ErrorsHandling Database ErrorsAlmost every implementation of WCF services uses a database. Typically, most errors in your service originate with that database. It is critical to identify the root cause of the errors as quickly as possible. You do not want to have to wait until a database administrator sets up a profile that tries to recapture an error that may well be intermittent. Capture as much information as possible when an error occurs. Include the connection string, the name of the stored procedure or SQL statement, as well as any parameters that are used. To capture this information, you must wrap every call to the database with a try…catch block. The following code is a useful extension to the System.Exception class that captures parameters from a Command object. Visual C# Exception Extension to Capture Command Information C# Copy ///
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples error handling in c# best practices We’re sorry. The content you requested has been removed. You’ll be auto redirected
Error Handling C# Mvc
in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded
C# Error Handling Example
Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table https://msdn.microsoft.com/en-us/library/hh323703(v=vs.100).aspx of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Complete Example for Error Handlers Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 This code example includes elements for both page-level and application-level exception handling. Code Example Files The example consists of the following files: Web.config Global.asax Default.aspx ExceptionUtility (to be put in the https://msdn.microsoft.com/en-us/library/bb397417.aspx App_Code folder) GenericErrorPage.aspx HttpErrorPage.aspx Http404ErrorPage.aspx DefaultRedirectErrorPage.aspx Web.config The following example shows the Web.config file. The customErrors section specifies how to handle errors that occur with file types that are mapped to ASP.NET, such as .aspx, .asmx, and .ashx files. (In IIS 6.0 and in IIS 7.0 in classic mode, static content files such as .html and .jpg files are not mapped to ASP.NET.) The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 errors would occur if a request were made for an .aspx file, .asmx file, and so on and if the requested file did not exist. All other unhandled errors in ASP.NET files are directed to the DefaultRedirectErrorPage.aspx file. If static content files are not handled by ASP.NET, a request for a nonexistent .html or .jpg file does not cause a redirect to the Http404ErrorPage.aspx file. If you want ASP.NET to handle requests for all file types, you can configure IIS to map file-name extensions to ASP.NET. Note In the example, the mode attribute is set to "On" so that you can error messages when you run the example i
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business http://stackoverflow.com/questions/4680845/try-catch-every-db-connection Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up try-catch every db connection? up vote 6 down vote favorite 1 Is it recommended to put a try-catch block in every function that error handling opens a DB connection and log the error there, or should I rather catch errors in a higher layer of the application? public static Category GetCategoryByName(string name) { Category result; try { using (IDbConnection conn = ConnectionHelper.CreateDbConnectionByName(_connectionStringName)) { conn.Open(); using (IDbCommand cmd = conn.CreateCommand()) { //do stuff } } } catch(Exception e) { // log error here? } return result; } or rather try { Category myCat = DataTools.GetCategoryByName("myCat"); // other stuff } catch(Exception e) c# error handling { // log error here? } To sum it up: Should errors be caught as early as possible in the code? Or should I rather catch them where I have more information about the context? c# .net error-handling try-catch share|improve this question asked Jan 13 '11 at 14:00 atticae 5,28763890 add a comment| 4 Answers 4 active oldest votes up vote 12 down vote accepted As always, it depends, but in general, only catch an exception if you can do something about it, or you have specific code (e.g. a retry) to happen, otherwise, let the exception bubble up and the top most layer can log it/deal with it in a centralised fashion. Any other way results in a lot of logging code interspersed with all the business logic. share|improve this answer answered Jan 13 '11 at 14:02 Paddy 22.9k95691 add a comment| up vote 4 down vote When catching exceptions, always try to use the most accurate exception you can. For example, when using SQL Server, catch the SqlException as it will contain far more information about the exceptin than a generic Exception. You can get actual line numbers and other useful pieces of diagnostic information. After you have extracted and logged all that is relevent, rethrow the exception or wrap it in less specific exception such as an InvalidDataException o