Doubt with Try Catch

Asked

Viewed 102 times

0

I have the following code:

try
{
modelOn.pacienteOns.Add(Conversao.pacienteToOn(oObjeto));
oObjeto.online = 1;
modelOn.SaveChanges();
modelOff.SaveChanges();
}
catch (Exception i)
{
MessageBox.Show("----------------->>> " + i);
}

I need the system to execute both commands modelOn.SaveChanges() and modelOff.SaveChanges(), if you do not execute one, you should not execute the other.

My question is: Since I included both codes within the try, both will always be executed or has the risk of executing only one?

  • 1

    If any of the lines above them burst an exception, you run the risk of neither of the two being executed. Exception interrupts execution and jumps straight pro catch.

  • That’s what I want. Or both or none. You confirm to me if that’s how it is anyway?

  • 1

    The reasoning is the same, and with this code there is no way to guarantee it. If modelOff.SaveChanges(); launch exception for some reason, Try code is stopped, but modelOn.SaveChanges(); will have already executed.

  • Is there another way to do? to run all or none?

2 answers

2


I got this code from Devmedia support:

var bancoOnTx = bancoOn.Database.BeginTransaction();
var bancoOffTx = bancoOff.Database.BeginTransaction();
try{
    //faz as transações
    bancoOn.SaveChanges(); 
    bancoOff.SaveChanges();
    bancoOnTx.Commit(); 
    bancoOffTx.Commit(); 
} 
catch (Exception e){ 
    bancoOnTx.Rollback();
    bancoOffTx.Rollback();
    MessageBox.Show(e);
}

2

The try catch can be seen as a protection against 'crash' to handle exception errors in a secure way. The program tries to execute what is in the block try and if you find an exception error, jump to the block catch. No mistakes, skip the block catch.

What you want (to execute a command conditionally) has nothing to do with this structure (which is protection against error and not conditional execution).

So you can do what you want, focus on the block and get your parole into it.

{
 modelOn.pacienteOns.Add(Conversao.pacienteToOn(oObjeto));
 oObjeto.online = 1;
 modelOn.SaveChanges();
 modelOff.SaveChanges();
}

Since we do not know what the methods return (if they return any value), I will propose a code to use example:

{
 modelOn.pacienteOns.Add(Conversao.pacienteToOn(oObjeto));
 oObjeto.online = 1;
 if (modelOn.SaveChanges()) modelOff.SaveChanges();
}

In this code, I’m assuming the method SaveChanges() of modelOn returns true was saved and false if not saved. Thus the method SaveChanges() of modelOn is only executed if the other has successfully executed.

  • 2

    What if modelOff.SaveChanges() do not perform, where modelOff.SaveChanges() already executed?

  • 3

    Besides what the diegofm already said, the last thing a try-catch should be seen as a way to avoid crash. Because of this view people use exceptions in the wrong way all the time. Precisely because of this people think that make a catch (Exception) It’s a good thing, they think that by doing this any problem that the application has will be solved, when it is usually the opposite that occurs. When more than 90% of people think this is right it will be unlikely that it will be solved, most continue teaching wrong those who want to learn and will teach other wrong too.

  • 1

    @diegofm, as I do not know what the methods do saveChanges(), I just wanted to answer your question, demonstrating that the try-catch will not guarantee this conditional execution. You will have to work the logic of the methods to be executed exclusively. You can use the transaction concept or a callback

  • 1

    @bigown, I agree that the try-catch is abused. Who never wanted to put all the blocks inside it? Edited

  • 1

    The saveChanges is part of the Entity Framework and produces database operations. It has try-catch, nor if will solve this. I don’t know exactly what the best solution is, but it certainly involves using the RU in a specific way so that every transaction is transactional. You may even need to change the model or make a gambit (or alternative logic as some prefer to call it), but here I’m just speculating. One might say that this method casts no exception, but there are no guarantees that a method has not launched exception indirectly.

  • 1

    The tip may be good, but it still won’t solve the problem of ensuring that both methods are always executed, as was questioned by the OP. It’s really complicated to suggest something just with past information.

  • the methods are used to record in the database. From what I read from your conversation, it is not as easy as it seems kkk. but thanks for the help

  • It’s also not difficult, but you didn’t give much information. I don’t have experience with C#, but try-catch is a common structure to other languages and clearly, is not the solution you seek. You can improve the question or ask a new question, with more data, taking care not to lose focus.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.