Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
based on this following code i am going to get data from indent table by using stored procedure with two arguments (indno, incharge). so whenever the system exceptions ( like sql exception, Exception etc,.) occurred then i want to handle with custom exceptions like DAO catch block.. but not working.. i dont know where i did mistake.

Thanks in advance..

What I have tried:

public DataSet GetIndentsDetails(int indno,string Incharge)
        {
            DataSet DsIndentDetails = new DataSet();
            try
            {
                if (dbConn == null)
                    dbConn = CommonProcedures.GetConnection();
                dbCmd = dbConn.GetStoredProcCommand("usp_GetAllIndents");
                dbConn.AddInParameter(dbCmd, "IndentNo", DbType.Int32, indno);
                dbConn.AddInParameter(dbCmd, "Incharge", DbType.String, Incharge);
                DsIndentDetails = (DataSet)dbConn.ExecuteDataSet(dbCmd);                
                return DsIndentDetails;
            }
            catch (DBConnOpeningException dbConnOpenEx)
            {
                
                throw new DBConnOpeningException("Unable to Connect Database");
               // return DsIndentDetails;
            }
            catch(DBSelectFailedException dbSelExc)
            {
                throw new DBSelectFailedException("Select Statement Failed");
            }
            catch (DAOException DAOEx)
            {
                throw new DAOException("DAOException Occured");
                //return DsIndentDetails;
            }
            catch (IndentsListException ilistEx)
            {
                throw new IndentsListException("Could not load Indent List");
                //return DsIndentDetails;
            }
                       
            catch(ERPException erpEx)
            {
                throw new ERPException("ERPException Occured");
            }
            finally
            {
                dbConn = null;
            }
        }



Custom Exception
--------------------
public class ERPException : System.Exception
    {
        public ERPException():base()
        {
           
        }

 
        public ERPException(string arg1,System.Exception arg2):base( String.Format( "{0}",arg1), arg2 )
        {

        }
        public ERPException(string arg1): base (String.Format("{0}",arg1))
        {

        }
        //public ERPException(System.Exception arg1):base(arg1)
        //{

        //}
        
    }



public class DAOException : ERPException
   {
       public DAOException(): base()
       {

       }
       public DAOException(string arg1,System.Exception arg2):base(String.Format("{0}",arg1), arg2)
       {

       }
       public DAOException(string arg1):base(String.Format("{0}",arg1))
       {

       }
   }
Posted
Comments
Richard Deeming 16-Jul-20 6:28am    
None of the exceptions you're catching are built-in exceptions. If you get a DbException, it won't be caught.
Mshareef880 18-Jul-20 1:30am    
ok..thanks for reply,,
Keith Barrow 16-Jul-20 10:06am    
Just to add to Richard Deeming's answer - if you want to catch say the DAOEXception you have to throw it first (or probably catch some DBException and throw the exception type you want. The framework won't automatically turn an Exception into a DAOException because one is subclassed by the other (assuming that is how you expect it to work).
Mshareef880 18-Jul-20 1:31am    
@keith barrow
Thanks :)
ZurdoDev 16-Jul-20 13:18pm    
Just catch regular Exception.

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