Click here to Skip to main content
15,885,985 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 6:40
Bootzilla3321-Sep-17 6:40 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3321-Sep-17 18:19
Bootzilla3321-Sep-17 18:19 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3322-Sep-17 2:54
Bootzilla3322-Sep-17 2:54 
AnswerRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 6:07
eddieangel19-Sep-17 6:07 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 6:14
Bootzilla3319-Sep-17 6:14 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 6:30
eddieangel19-Sep-17 6:30 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 6:58
Bootzilla3319-Sep-17 6:58 
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
eddieangel19-Sep-17 7:17
eddieangel19-Sep-17 7:17 
Your model class already exists, it is the Data.General.Shipment object. All of this is really outside the scope of your original question of multiple try... catch... blocks, though. If you are able it is always good to step back and ask yourself if this is the right approach. DO you really need XML? It is super wordy and a pain to work with, JSON is way better, but again, outside the scope.

To put an easy stamp on your original question, you can have many try catch blocks but it is going to be a very iterative process for you to figure them out and consider all of the permutations. So you have to ask yourself what the goal is. Is there a recovery path through these exceptions or are you trying to send different error messages to the user? Or maybe just log more detail?

Either way if your try/catch is at the point of building out the XML all you are likely to get is XML formatting exceptions, which do not encapsulate your business rules. A couple notes on generally being defensive in the code you have written:

1. Do not use decimal.Parse. Since you are using the null check operator getting the tracking number, you expose the possibility of errors there. Try this:

C#
var prc = new Data.General.Shipment();
                    var node = mainNode.SelectSingleNode("TrackingNumber");

                    if (node == null)
                    {
                        return; // If your method is not void return the appropriate data type, null, false, etc...
                    }

                    decimal trackingNumber;

                    if (!decimal.TryParse(node.InnertText, out trackingNumber))
                    {
                        return; // Same as above with return type
                    }

                    prc.GetByProNumber(trackingNumber);

                    try
                    {
                        rspxml.Root.Add(new XElement("API", "4.0"));
                        rspxml.Root.Add(new XElement("PackageTrackingInfo"));
                        rspxml.Root.Element("PackageTrackingInfo").Add(new XElement("TrackingNumber", prc.ProNumber.ToString())); // These ToString calls are dangerous without first validating the data
                        rspxml.Root.Add(new XElement("PackageDestinationLocation"));
                        rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("City", prc.Consignee.ToString()));
                        rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("StateProvince", prc.Consignee.ToString()));
                        rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("PostalCode", prc.Consignee.ToString()));
                        rspxml.Root.Element("PackageDestinationLocation").Add(new XElement("CountryCode", prc.Consignee.ToString()));
                        // remainder omitted for brevity
                    }
                    catch (NullReferenceException n)
                    {
                        // NullReferenceException is not super helpful but it will tell you there is an error and provides a general catch all.
                        // Log or alert user there is a problem
                        // A more defensive strategy is to validate that prc.Consignee is not null before trying to covnert to a string
                    }


At very least, make sure you validate that the tracking number comes up and hydrates an object.
GeneralRe: How to handle multiple exceptions(Try..Catch) Pin
Bootzilla3319-Sep-17 7:28
Bootzilla3319-Sep-17 7:28 
QuestionXSL to get only elements which have no childs Pin
MrKBA18-Sep-17 23:27
MrKBA18-Sep-17 23:27 
AnswerRe: XSL to get only elements which have no childs Pin
Pete O'Hanlon18-Sep-17 23:49
mvePete O'Hanlon18-Sep-17 23:49 
GeneralRe: XSL to get only elements which have no childs Pin
MrKBA19-Sep-17 0:26
MrKBA19-Sep-17 0:26 
Questioni need Visual Studio .NET 2002 prerequisites cd Pin
michael nabil18-Sep-17 23:05
michael nabil18-Sep-17 23:05 
AnswerRe: i need Visual Studio .NET 2002 prerequisites cd Pin
OriginalGriff18-Sep-17 23:14
mveOriginalGriff18-Sep-17 23:14 
QuestionMessage Closed Pin
16-Sep-17 18:15
professionalHardevsinh Mori16-Sep-17 18:15 
AnswerRe: SQLTransaction Pin
OriginalGriff16-Sep-17 19:57
mveOriginalGriff16-Sep-17 19:57 
GeneralRe: SQLTransaction Pin
Hardevsinh Mori16-Sep-17 20:24
professionalHardevsinh Mori16-Sep-17 20:24 
GeneralRe: SQLTransaction Pin
Hardevsinh Mori16-Sep-17 20:26
professionalHardevsinh Mori16-Sep-17 20:26 
GeneralRe: SQLTransaction Pin
OriginalGriff16-Sep-17 21:44
mveOriginalGriff16-Sep-17 21:44 
GeneralRe: SQLTransaction Pin
Hardevsinh Mori16-Sep-17 21:47
professionalHardevsinh Mori16-Sep-17 21:47 
GeneralRe: SQLTransaction Pin
OriginalGriff16-Sep-17 21:49
mveOriginalGriff16-Sep-17 21:49 
GeneralRe: SQLTransaction Pin
Hardevsinh Mori16-Sep-17 21:51
professionalHardevsinh Mori16-Sep-17 21:51 
GeneralRe: SQLTransaction Pin
OriginalGriff16-Sep-17 21:58
mveOriginalGriff16-Sep-17 21:58 
Questionc# project Pin
Member 1341347616-Sep-17 11:26
Member 1341347616-Sep-17 11:26 
AnswerRe: c# project Pin
Afzaal Ahmad Zeeshan16-Sep-17 11:57
professionalAfzaal Ahmad Zeeshan16-Sep-17 11:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.