Answer the question
In order to leave comments, you need to log in
How to avoid brakes when try catch is triggered?
Good afternoon, dear colleagues.
There is a table with a bunch of records in which the Subscriber field is represented as a phone number and there is a table of correspondence between the phone number and the full name (Subscriber field and FIO). The point is that not all subscribers have full name designations, as a result of which exceptions (Exceptions) will necessarily fall out, which I process and leave there just a phone number.
I am fetching from SQL like this:
SELECT TOP 100 Calls.ID, Calls.DateTime, Calls.Trunk, Calls.Subscriber, Subscribers.FIO, Calls.ExtNum, Calls.Duration, Calls.Direction, Calls.ExitCode FROM Calls LEFT OUTER JOIN Subscribers ON Calls.Subscriber = Subscribers.Subscriber ORDER BY ID DESC
while (reader.Read())
{
rec = new CDRRecord();
rec.ID = reader.GetInt64(0);
rec.DateTime = reader.GetDateTime(1);
rec.Trunk = reader.GetString(2);
//Если значение из Subscribers.FIO не Null, присваиваем его записи,
//если null - будет Exception
try
{
rec.Subscriber = reader.GetString(4);
}
//тогда присваиваем записи значение Calls.Subscriber
catch
{
rec.Subscriber = reader.GetString(3);
}
rec.ExternalNumber = reader.GetString(5);
rec.Duration = TimeSpan.FromTicks(reader.GetInt64(6));
rec.Direction = reader.GetString(7);
rec.ExitCode = reader.GetString(8);
records.Add(rec);
}
catch(SqlNullValueException)
Answer the question
In order to leave comments, you need to log in
Why do you need not only exceptions, but also logic in the application in general? This behavior is elementarily specified in the request.
SELECT TOP 100 Calls.ID, Calls.DateTime, Calls.Trunk,
coalesce(Subscribers.FIO, Calls.Subscriber) as fieldname, -- если FIO NULL, тогда использовать значение поля Subscriber
...
Call IDataRecord.IsDBNull(int) instead of try-catch before calling .
Do not use exceptions to handle the flow of program execution. Exceptions are expensive. Do a normal check instead.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question