I have a problem regarding try catch when creating an object in C#.
My problem appears when an object is supposed to be created and the webservice which defines the object isn't available or has been modified. That is the problem I would like my program to handle.
When I try to do this:
try
{
var Customer = new Customer();
}
catch
{
//handling the exception.
}
Later on in my program I need that particular object, but due to the try catch the object isn't available in all cases (of course when the try fails, the object isn't created).
without if(customer != null)
if (insertCustomer(lead, LS_TS, out Customer) == true)
{
insert = true;
}
with if(customer != null)
if(customer != null)
{
if (insertCustomer(lead, LS_TS, out Customer) == true)
{
insert = true;
}
}
No matter what, the compiler says: "The name 'Customer' does not exist in the current context
How do I solve this problem? I need a program to be running all the time and when checking an object which isn't created then I need to exit the method and try again later.
Thanks in advance
You can just do this:
Customer customer = null;
try
{
customer = new Customer();
}
catch
{
// handling the exception.
}
and whenever you need to use the object customer you should do this
if(customer != null)
{
// do stuff
}