2
I have a doubt, I tried to find some content on Google but I did not find.
I have a block try catch
as follows:
try
{
//código
string idFormaPgto = Dictionaries.FormaPgto[_order.FK_PEDIDOS_FORMA_PAGAMENTO];
//restante do código
}
catch(Exception e)
{
}
The problem is sometimes I’ll get one KeyNotFoundException
but the following code MUST continue and I need to set a standard vallor to idFormaPgto
, I don’t know if there’s a more elegant and correct way to treat this, what I thought was:
try
{
//código
string idFormaPgto = "";
try
{
idFormaPgto = Dictionaries.FormaPgto[_order.FK_PEDIDOS_FORMA_PAGAMENTO];
}
catch(Exception e)
{
idFormaPgto = "1"; //tratamento do idFormaPgto
}
//restante do código
}
catch(Exception e)
{
}
My code solves the problem, but I believe it is not the most correct way to do this, perhaps using some class extension method Dictionary
but I don’t know any. The goal is to treat this KeyNotFoundException
unused try catch
inside try catch
But the other catch will do something?
– Jéf Bueno
Go, is that so... will be several queries within the main catch, some queries may result in Exception others not, for example this call in Dictionary can’t fall in the main catch, the others yes, so I’m treating her in specific, but I think it’s "pork"
– Leonardo Bonetti
It seems to me that the appropriate treatment would be for you to catch the exception you already expect... in case the
KeyNotFoundException
– Leandro Angelo
So why not check if the key exists before using?
– Jéf Bueno
@LINQ is a possible solution, but did not want to use blocks, like Try catch, if Else or whatever knows ? Do you know any function in the style "If there is no return this" all in one line ? as if it were a Firstordefault
– Leonardo Bonetti
@Leonardobonetti I know, I’ll try to write to you =)
– Jéf Bueno
@LINQ Okay, no waiting
– Leonardo Bonetti
I will improve the response with examples as soon as possible. Now I need to run to a meeting.
– Jéf Bueno