处理Catch Block中的数据库连接和网络问题


private void ProcessInputQueueEntry(long parentNodeId, ExperianInputQueue eiq, UserInfo     uinf)
        {
            try
            {
                logger.InfoFormat("Processing InputQueue entry: {0}, Major: {1}, Minor:     {2}", eiq.Id, eiq.MajorType,
                                  eiq.MinorType);
                var parser = ExperianInputXMLParserFactory.Construct(eiq);
                // TODO: Try-catch + log the eiq ID + new status code for this eiq.
                switch (eiq.MajorType)
                {
                    case InputMajorType.ReportCases:
                        switch (eiq.MinorType)
                        {
                        case 1: // 1.1 -- Schema 2.1.1
                                CreateNewRegistration((ExperianReportCase)parser.GetObject(), parentNodeId, uinf);
                                break;
                            case 2: // 1.2 -- Schema 2.2.1
                                DeleteRegistration(parser, uinf);
                                break;
                            case 3: // 1.3 -- Schema 2.3.1
                                MoveToSolicitor(parser, parentNodeId, uinf);
                                break;
                            case 4: // 1.4 -- Schema 2.4.1
                                MoveToClient(parser, parentNodeId, uinf);
                                break;
                            case 5: // 1.5 -- Schema 2.5.1
                                Reopen(parser, parentNodeId, uinf);
                                break;
                            case 6: // 1.6 -- Schema 2.7.1
                                MoveToPublicService(parser, parentNodeId, uinf);
                                break;
                        }
                        break;
                    case InputMajorType.Cpr:
                        switch (eiq.MinorType)
                        {
                            case 1: // CPR: 2.1 
                                DeleteRegistrationsFromCpr(eiq.IdCardNo, uinf);
                                break;
                            //case 80: // CPR: 2.80 --UDR (The person has left Denmark)
                            //case 70: // CPR: 2.70 --FSV( The person has disappeared)
                            case 2: // CPR: UDR (Teh person has left Denmark)
                            case 3: // CPR: FSV( The person has disappeared) --UDB(The     person no longer has a permanent residence)
                            case 4: // CPR: UDB(The person no longer has a permanent     residence)
                            case 5:
                            case 6: // CPR: Call RKI
                            case 7:
                                try
                                {
                                    HandleCPRStatusUpdateOtherThanDeath(eiq.IdCardNo, uinf, eiq.MinorType);
                                }
                                catch (Exception exception)
                                {
                                    logger.ErrorFormat("An error occurred while updating     the cpr status for the InputQueue task for eiq ID = {0} -- stacktrace ---> {1}", eiq.Id,     exception);
                                    throw exception;
                                }
                                break;
                        }
                        break;
                    case InputMajorType.CustomerDeletion:
                        switch (eiq.MinorType)
                        {
                            case 1: // DeleteCustomer: 3.1
                                DeleteRegistrationsByCustomer(eiq.ExperianCustomerNumber, uinf);
                                break;
                        }
                        break;
                    case InputMajorType.IdCardCombined:
                        switch (eiq.MinorType)
                        {
                            case 1: // CombineIdCards: 4.1
                                CombineIdCards(parser, parentNodeId, uinf);
                                break;
                        }
                        break;
                    case InputMajorType.Document:
                        switch (eiq.MinorType)
                        {
                            case 1: // 5.1
                                AddDocumentToNode(parser, eiq.NodeId, eiq.Data, uinf);
                                break;
                            case 2: // Pass documents to a Feedback dossier node through     Upload Document.
                                AddDocumentToFeedbackDossierNode(parser, eiq.NodeId, uinf);
                                break;
                        }
                        break;
                    case InputMajorType.CustomerContractTerminated: //TTP1256
                        if (eiq.MinorType == 1)
                        {
                            HandleCustomerContractTermination(eiq.ExperianCustomerNumber, uinf);
                        }
                        break;
                        default:
                        break;
                }
                eiq.Status = 1; // Processed...
                UpdateInputQueueItem(eiq, uinf);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("Error while executing EsdhInputQueue task for eiq ID     = {0} -- stacktrace ---> {1}", eiq.Id, ex);
                eiq.Status = 0; // Status = ErrorProcessing! //#1131 Making the state to     0 if the failure is due to network or web service is down so that i can process next time
            //Added for 2134
                EventLog.WriteEntry("Task Scheduler", "An Error has occured while     processing the input queue case", EventLogEntryType.Error, 2);
                UpdateInputQueueItem(eiq, uinf);
            }
        }

上面列出了我的代码。。。。在最后一个捕获块中。。。。如果存在任何数据库连接问题或任何网络访问问题(例如无法从服务器端获取数据或数据库有问题(,我需要捕获异常,并将相应的状态设置为1,以便稍后进行处理。我该如何将其作为if语句添加到结束catch块中呢。

您需要捕获所有试图捕获的特定异常,从最特定到最通用的开始

try
{
 //Some Code
}
catch (CommunicationException ex)
{
//Do something
}
catch (AnotherException ex)
{
//Do something
}

顺便说一句,捕获基本异常类

通常是个坏主意

通常,当我们处理异常时,我们必须始终从上下文中更具体的一个开始,到更一般的一个,即Exception

try
{  
 //Your code
}
catch (OracleException orEx)
{
}
try
{  
 //Your code
}
catch (WebException wbEx)//if via web app
{
}
 try
{  
 //Your code
}
catch (SocketException skEx)//The exception that is thrown when a socket error occurs.
{
}
try
{  
 //Your code
}
catch (Exception Ex)//the more general exception
{
}

最新更新