Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Exception Handling » Exception and error handling in enterprise applications

Exception and error handling in enterprise applications

In this article, I try to cover the key principles and the main techniques of exception and error handling with examples in SQL Server, Oracle and Microsoft .NET.

Author Rank:
Technologies: .NET 2.0, SQL Server 2005,Visual C# .NET
Total downloads :
Total page views :  5722
Rating :
 5/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Become a Sponsor


Related EbooksTop Videos

Introduction

Exception and error handling is an important feature in any robust enterprise application. A lot of developers misunderstand and not apply the underlying techniques. Therefore, the end user is confused and does not know what to do when an exception occurs. It's responsible of the developer to try to handle the more exceptions as he can in order for the data to be consistent as well as to free the end user to make decision related to the software application. In this article, I try to cover the key principles and the main techniques of exception and error handling with examples in SQL Server, Oracle and Microsoft .NET.

Exception and error handling in Microsoft SQL Server

First of all, it's remarkable to say about the new features of Microsoft SQL Server implementing error handling in an elegant way by using the TRY...CATCH statement.

In SQL Server, we deal with two types of errors: built-in error and user-defined error. Built-in errors are those raised by response to standard SQL Server system's errors. User-defined errors are those used by third-party applications to define customized errors to be used within the application. An error has properties such as an identifier, a severity and a text.

The severity ranges from 1 to 25, and we can find some categorization such as:

  • 0-10. Indicates an informational messages.
  • 11-16. Database system errors which can be corrected by the user. For example, errors that affect the integrity of the database.
  • 17-19. Database system errors which need for the administrator attention.
  • 20-25. Fatal errors such as hardware and software crashes.

The messages of these errors are stored in the sys.messages table. You can see the error messages defined in your system using the following query (see Listing 1).

select message_id, severity, is_event_logged, text
from sys.messages


Listing 1.

 

It's possible to add new user-defined message error to the former table using the sp_addmessage stored procedures. Let's define a message error indicating a prohibition to delete a table (see Listing 2).

use master;

go

 

exec sp_addmessage 100001,14,N'You need more privelege to delete the table %s';
go


Listing 2.


The first parameter indicates the message identifier whose value is between 50001 and 2147483647. The second parameter indicates the severity level, and the last parameter is the actual message text.


Now, let's supposed that we have detected that a user is trying to delete a very important table of our application, and then we send an error message using the command RAISERROR (see Listing 3).

raiserror(100001, 14, 1, N'admin_schema.important_table');

go


Listing 3.


The RAISERROR command is often used to return error message to business logic components of the enterprise application, for example, when the integrity of some entities is violated.


You can also raise an error message without defining the message (see Listing 4).


raiserror('You need more privelege to delete the table %s', 14, 1, N'admin_schema.important_table');


Listing 4.


Up to this point, we've seen how to define error message. Now, let's see how to handle the errors.


Prior to SQL Server 2005, error handling was done by checking the @@ERROR global variable after each statement was executed, and then using a GOTO statement (the worst of the practices) the logic was interrupted in order to go a centralized error-handling block of code (see Listing 5).

 

create table test(x int check(x>0));

go

 

begin tran

 

insert into test values(1);

if @@ERROR <> 0 goto Error_Handling_Code;

 

insert into test values(-1);

if @@ERROR <> 0 goto Error_Handling_Code;

 

insert into test values(2);

if @@ERROR <> 0 goto Error_Handling_Code;

 

commit tran;

 

Error_Handling_Code:

print 'Error occurred.';

rollback tran;

go


Listing 5.


Now, the TRY...CATCH statement can be used to capture execution errors in T-SQL code. The syntax is as follows (see Listing 6).


begin try
  {sql_statement | statement_block}
end try
begin catch
  {sql_statement | statement_block}
end catch


Listing 6.


If an error is encountered within the try block, then we go directly to the catch block without processing other statements in the try block. It's remarkable to notice that warning and informational messages (severity level less than 10) are not captured by the TRY...CATCH statement. You can also nest a TRY...CATCH statement inside other TRY...CATCH statement.


Let's rewrite the code in Listing 5 using the TRY...CATCH statement (see Listing 7).

create table test(x int check(x>0));

 

begin try

  begin tran;

  insert into test values(1);

  insert into test values(-1);

  insert into test values(2);

  commit tran;

end try

begin catch

  print 'Error ocurred. Message '+ERROR_MESSAGE();

  rollback tran;

end catch


Listing 7.


It's remarkable to say that SQL Server by default does not rollback the changes to database objects once an exception occurs. It's your decision as developer to explicitly rollback the changes. Although if you set the XACT_ABORT option to ON, then transactions are rolled back in the event of any runtime error.


Exception and error handling in Oracle database


In Oracle, we can find a good mechanism to work with exceptions. There are two types of exceptions:

  • System exception. This exception is defined by Oracle and raised by the PL/SQL engine when an error is detected.
  • Programmer-defined exception. An exception specific to the application and raised for some events of the application.

In PL/SQL, we can catch and handle the exceptions using the exception section (which is optional) associated to PL/SQL blocks. This section is used to handle any error that occurs during the execution of PL/SQL code inside the block. It's a way to cleanly separate the error processing logic from your business logic. The syntax for the exception section is as follows (see Listing 8). The section can have as many WHEN statements as you like to handle specific exceptions; all other exceptions are handled by the WHEN OTHERS section.


EXCEPTION
WHEN exception_name
THEN
 error_handling_code
[WHEN OTHERS default_exception_handling]


Listing 8.


Like the TRY...CATCH statement in T/SQL, the exception section in PL/SQL provides an event-driven model for processing errors, because no matter what exception is raised (an application or user-based), and then the processing of the business logic is halt, and the control is transferred to the exception section. The control does not return to that part of the block once you have finished handling the error.


Before an exception is raised, it must be defined. In Oracle, you can find thousands of built-in exceptions (assigning numbers and messages). The most commonly used exceptions are found in the STANDARD package of PL/SQL.


Because many problems, that you may encounter, are specific to your solution, then you must declare your own exceptions (user-defined exceptions) in the declaration section using the following syntax (see Listing 9).


exception_name EXCEPTION;


Listing 9.


If you want to initiate an exception, you must follow the syntax (see Listing 10).


RAISE exception_name;


Listing 10.


And if you want to handle this specific exception, you must use the WHEN clause as illustrated (Listing 11).


WHEN exception_name THEN


Listing 11.


In addition, developers can raise exceptions using the RAISE_APPLICATION_ERROR built-in procedure and passing an error number (between -20000 and 20999) and an error message. The main advantage of this procedure is to associate an error message with the exception. When this procedure is invoked the changes made to parameters (IN OUT and OUT) are reversed while changes made to database objects (using SQL statement such as INSERT, UPDATE, DELETE) will not be rolled back. You must execute an explicit ROLLBACK statement.


When an exception is caught, then you can use several built-in functions to analyze the errors:

  • SQLCODE. Returns the error code of the most recent exception. If there is no error, then it returns 0.
  • SQLERRM. Returns the error message for a particular error code. If SQLCODE is 0, then it returns “ORA-0000: normal, successful completion”. You can see an example (see Listing 12).
  • DBMS_UTILITY.FORMAT_ERROR_BACKTRACE. This function is new to Oracle 10g and returns a formatted string displaying the stack of the program and line number to the line on which the error is originated. Let's see an example (see Listing 13).

begin
    dbms_output.put_line(SQLERRM(-1));
end;
/


Listing 12.


create or replace procedure proc1 is
begin
    dbms_output.put_line('This is proc1');
    raise no_data_found;
end;
/
create or replace procedure proc2 is
begin
    dbms_output.put_line('This is proc2');
    proc1;
end;
/
create or replace procedure proc3 is
begin
    dbms_output.put_line('This is proc3');
    proc2;
 exception
   when others
   then
       dbms_output.put_line(DBMS_UTILITY.format_error_backtrace);
end;
/
set serveroutput on;
begin
    dbms_output.put_line('Testing back trace');
    proc3;
end;
/


Listing 13.


Now let's rewrite the business logic in Listing 7 using the exception handling semantics in PL/SQL (see Listing 14).


create table test(x int check(x>0));
/
begin
     insert into test values(1);
     insert into test values(-1);
     insert into test values(2);
     commit tran;
exception
    when others
    then
        dbms_output.put_line('Error on your application logic');
        rollback tran;
end;


Listing 14.


Error handling in applications


Sometimes, the developer must deal with exceptions in their applications. Now we're going to show how to catch errors and warning in ADO.NET in order to be shown in our client applications.


Let's see how we can do it. When an exception is raised using RAISERROR, if the severity value is less or equal to 10, then it's generated a warning not an exception. The warnings are caught in the InfoMessage event of the Connection object. If the severity value is greater than 10, then an exception is raised and this exception is caught using the TRY...CATCH...FINALLY mechanisms of C#.


To illustrate these concepts, let's create a Windows form, and add a StatusStrip control to the form. Then inside the StatusStrip control, let's add a ToolStripStatusLabel control to display any warning sent by the database system (in our case, SQL Server) to our application. Finally, let's add a button to invoke a stored procedure in the backend server which sends warning and exceptions. The errors are displayed using a DialogBox.


First of all, let's define a stored procedure which returns a warning (see Listing 15).

create procedure spTestProc

as

begin

   raiserror(N'This is a warning', 10, 1);

end;


Listing 15.


Now, let's define the C# code of client application (see Listing 16).


 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace ExceptionHandlingWinForms

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

        {

 

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            using (SqlConnection objConnection = new SqlConnection("Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True"))

            {

                objConnection.InfoMessage += new SqlInfoMessageEventHandler(objConnection_InfoMessage);

 

                try

                {

                    SqlCommand objCmd = new SqlCommand();

                    objCmd.Connection = objConnection;

                    objCmd.CommandType = CommandType.StoredProcedure;

                    objCmd.CommandText = "spTestProc";

 

                    objConnection.Open();

                    objCmd.ExecuteNonQuery();

                }

                catch (SqlException objEx)

                {

                    System.Windows.Forms.MessageBox.Show(objEx.Message, "SQL Server error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

                finally

                {

                    objConnection.Close();

                }

            }

        }

 

        void objConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e)

        {

            this.m_tsLWarning.Text = "Warning: "+e.Message;

        }

    }

}


Listing 16.


When you execute the application and press the button, the result is as shown (Figure 1).

 

 

Figure 1.


Let's change the code of the stored procedure in order to return an exception (see Listing 17).

alter procedure spTestProc

as

begin

   raiserror(N'This is an exception', 11, 1);

end;

 

Listing 17.


Now, let's execute the application again and see the results (as shown in Figure 2).

 


 
Figure 2.


You can apply the same business logic to Oracle client application. Let's define a procedure in PL/SQL which returns an application error message to the application (see Listing 18).


create or replace procedure spTestProc
as
begin
  raise_application_error(-20010,'Sending an application error');
end;
/


Listing 18.


When you run your application, then you will receive and handle this error using TRY...CATCH...FINALLY mechanisms.


Conclusion.


In this article, I explained the principles of exception and error handling in enterprise application. In the first part, I talked about defining exception and handling this exception in relational database systems such as SQL Server and Oracle. The second part, I explained how you can catch warnings and exceptions, not handled in the server-side code, which must be handled by the application developer on the client side.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
John Charles Olamendy
He’s a senior Integration Solutions Architect and Consultant. His primary area of involvement is in Object-Oriented Analysis and Design, Database design , Enterprise Application Integration, Unified Modeling Language, Design Patterns and Software Development Process. He has knowledge and extensive experience in the development of Enterprise Applications using Microsoft.NET and J2EE technologies and standards. He is proficient with distributed systems programming; and business-process integration and messaging using the principles of the Services Oriented Architecture (SOA) and related technologies such as Microsoft BizTalk Server, Web Services (Windows Communication Foundation, WSE, BEA WebLogic, Oracle AS and Axis) through multiple implementations of loosely-coupled system. He’s a prolific blogger contributing to .NET and J2EE communities and actively writes articles on subjects relating to integration of applications, business intelligence, and enterprise applications development. He holds a Master’s degree in Business Informatics at Otto Von Guericke University, Magdeburg, Germany. He was recently awarded as MVP. He currently works in the telecommunication industry and delivers integration solutions for this industry. He harbors a true passion for the technology.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor
 Comments
Good work by DotNetGuts On September 10, 2008
Good work, for asp.net exception handling check out this link http://dotnetguts.blogspot.com/2007/10/error-handling-in-net-with-example.html
Reply | Email | Delete | Modify | 
Re: Good work by John Charles On September 29, 2008

Thanks. I will check the URL and I might also write an article about this topic.

John.

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved