Home > Back-end >  Consult programming problems try catch and assert
Consult programming problems try catch and assert

Time:10-15


Write programs sometimes lazy, especially in algorithm hasn't confirm whether can work, particularly don't like to spend time on programming standards, total want to wait for confirmation algorithm can work, and make the code standardization, sure this is a bad habit, should be corrected slowly, if today were the programmer colleagues complain , worst of all, he said, I don't quite understand the specific situation is in the parentheses after the code I try a piece of code cause the program crash. Our programmers give me feedback as follows, said he never use empty catch blocks, and he will use assert immediately see what when debugging, please see the following reply, the original I know with try catch, also know assert, but didn't get what he wanted me to do, feel this is a very simple question, embarrassed to ask him directly, first to consult everybody, first thank you
Try
{

//one line here under caused crash

}
The catch (... ) {}

Our programmers to reply the following

There is a coding mantra that says do not use the empty catch blocks - I almost always put in an assert if nothing else so you see them immediately when was debugging!

CodePudding user response:

In catch nothing words is equivalent to ignore the abnormal bai, the resulting effect depending on the code logic, sometimes may not have any effect, sometimes may be a disaster, so when you decide to catch it and do nothing, it is important to ensure that your try even if there are abnormal, your program can run in the form of user can accept, I think you said "he never in an empty catch blocks, and he will use assert when debugging immediately see what" meaning should be in the catch (... ) {assert (false); } mean, because catch what all need not do that even if an exception occurs in the try, the system can run normally, and programmers think normal logic abnormal case will not happen in the try, so in the catch and assert convenient debugging, commissioning stage, after all, what could happen,

CodePudding user response:

Thank you very much for your reply

So we programmers is suggested that if I don't know what catch words, always do

Try
{
}
The catch (... )
{
Assert (false);
}

But I tried it on in program, deliberately produce an exception, with the assert (false); And there is no assert (false); There was no difference ah, why bother to tell, thank you

CodePudding user response:

refer to the second floor karen99 response:
thank you very much for your reply

So we programmers is suggested that if I don't know what catch words, always do

Try
{
}
The catch (... )
{
Assert (false);
}

But I tried it on in program, deliberately produce an exception, with the assert (false); And there is no assert (false); No difference ah, why bother to tell, thank you

Assertions in the debug mode will only take effect

CodePudding user response:

3 floor right, assert is the debug mode into effect, is self checking code to the programmer, convenient debugging,

CodePudding user response:

The meaning of the literal translation
There is a coding mantra that says do not use the empty catch blocks -
In the programming jargon, do not use empty catch block
I almost always put in an assert if nothing else so you see them immediately when was debugging!
I almost always put a assert statement if there is no other treatment, so you can quickly see in the debug this information

So, not necessarily is put in the catch a assert (false), can assert according to the circumstance variables (key)

  • Related