need help with exception handiling

Last post 11-06-2009 12:58 PM by ghassan_aljabiri. 7 replies.

Sort Posts:

  • need help with exception handiling

    11-01-2009, 3:42 PM

    hello

    i use

    try

    catch

    end try

    ====================

    in try section there are more than one excepetced error

    in the catch section an action sould be done depending on the error type

    how could i determine what error happens there?

     

    Regards 

    GHS
  • Re: need help with exception handiling

    11-01-2009, 4:16 PM
    Answer
    • Contributor
      5,276 point Contributor
    • sukumarraju
    • Member since 06-14-2006, 6:01 PM
    • Scotland, UK
    • Posts 1,151

    ghassan_aljabiri:
    how could i determine what error happens there?
     

     

    You can catch specific exceptions in your Catch block as shown below. And as always common exception is handled as the last one.

     

    Catch(Exception ex)
    {
              if (ex is System.Security.SecurityException)   
    {   
            //Take action as you wish   
        }   
    else  if
       (ex is System.Data.SqlException)
    {
        //Take the action
    }
    
    }


     

    -- "Mark As Answer" if my reply helped you --
    Recommended Book
    Application Architecture Guide 2.0
    My Blog
  • Re: need help with exception handiling

    11-01-2009, 4:25 PM
    Answer
    • Contributor
      3,384 point Contributor
    • Danish Ali
    • Member since 08-08-2008, 11:22 PM
    • Fort Lauderdale, US
    • Posts 468

    you can do it using following approach. The benefit of this approach is that when exception will occur, it will be catched by its corresponding catch block. This approach is faster and most recommended.


    try        
            {
                // some code here
            }
            catch (SqlException sqlex)
            {
                // do something here
            }
            catch (FieldAccessException fexp)
            {
                // do something here
            }
            catch (Exception ex)
            {
                // do something here
            }

    try        

            {

                // some code here

            }

            catch (SqlException sqlex)

            {

                // do something here

            }

            catch (FieldAccessException fexp)

            {

                // do something here

            }

            catch (Exception ex)

            {

                // do something here

            }


    Hope it will help.

    If my post solves your problem, please mark it as an answer.
  • Re: need help with exception handiling

    11-03-2009, 2:47 PM

    yes that is greate start , but what if i want it with more specification

    i mean , an sqlexception may happen because a dublicatied primary key , table not found , connection error , i need to handle every one in a special way

    in VB 6 i used to use err.number

    where every error has its code

    is there a similer thing in vb.net (asp.net) 

    GHS
  • Re: need help with exception handiling

    11-03-2009, 2:54 PM
    • Contributor
      3,384 point Contributor
    • Danish Ali
    • Member since 08-08-2008, 11:22 PM
    • Fort Lauderdale, US
    • Posts 468

    Yes we have the Err.Number in vb.net as well.

    Please view the following link for details.


    http://msdn.microsoft.com/en-us/library/5hsw66as.aspx

    If my post solves your problem, please mark it as an answer.
  • Re: need help with exception handiling

    11-03-2009, 3:06 PM
    • Participant
      1,213 point Participant
    • mohammed askar
    • Member since 09-01-2009, 4:23 AM
    • Sri Lanka
    • Posts 296

    Dont relay your code with exception.

    Exception is very expensive in Asp..net application.

    It is not a good practice. Try to handel  your application process with programming logic.

     

    Please mark as answered, if this help you.
  • Re: need help with exception handiling

    11-05-2009, 9:30 AM
    Answer
    • All-Star
      62,990 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 1:34 PM
    • England
    • Posts 12,308
    • TrustedFriends-MVPs

    ghassan_aljabiri:
    A sql exception may happen because a duplicated primary key, table not found, connection error.  I need to handle every one in a special way

    Some of these errors should not happen. For example:

    • Primary keys should be identity integers. As they are system derived, duplication is not possible.
    • Table not found - are you using dynamic TSQL to generate table names?
    • Connection error - if a class fails to connect to the database, it should throw an InvalidOperationException with a message citing the connection string used.

    I suggest you get a copy of the Framework Design Guidelines from http://www.amazon.com/exec/obidos/tg/detail/-/0321246756/qid=1126565222/sr=8-1/ref=pd_bbs_1/102-0703728-8921753?v=glance&s=books&n=507846 and study chapter 7.

    The second edition is available from:

    http://www.amazon.com/Framework-Design-Guidelines-Conventions-Libraries/dp/0321545613/ref=dp_ob_title_bk/190-4796535-3943148

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: need help with exception handiling

    11-06-2009, 12:58 PM

    hello

    TATWORTH:
  • Primary keys should be identity integers. As they are system derived, duplication is not possible.
  •  

    if inserted data is taken automatically from another database that apply no restrections on the id field and u want to use the same feild as a primary key in the new database , and u want to discard the duplicated ids , you need a way to catch a dublicated primary key

     

    TATWORTH:
  • Table not found - are you using dynamic TSQL to generate table names?
  •  

     if the database is created by you , but tables is being created by a script (a user should run it) so you may expect a user to try to insert data before creating his tables , I know there are too many ways to prevent this with code , but I choosed this method!

     

    TATWORTH:
  • Connection error - if a class fails to connect to the database, it should throw an InvalidOperationException with a message citing the connection string used.
  • this one was helpfull , thanks alot

    TATWORTH:

     this one was very helpfull , thanks alot

    :|

    GHS
Page 1 of 1 (8 items)