NO

Author Topic: Com port  (Read 12202 times)

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Com port
« Reply #15 on: February 14, 2012, 07:40:54 PM »
try..catch is a nice feature for manged code, but for pure C I still prefer to know the problems I may encounter ;)
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: Com port
« Reply #16 on: February 14, 2012, 07:58:04 PM »
try..catch is a nice feature for manged code, but for pure C I still prefer to know the problems I may encounter ;)

Well there is that... Which is why I included the ReportError() function in my Error-X library...

The poject workspace is attached... take a look at the source code for the test program... Nothing stops you from reporting the errors when except{} occurs.  The ReportError() function searches Message Tables... first in your App or DLL resources, then in Windows DLLs and finally defaults to it's own set of messages...  You also have the option to terminate the program or not.
 
Anyway... having hijacked czerny's thread here... we should get back to the problem he's having with comports...

 

aardvajk

  • Guest
Re: Com port
« Reply #17 on: February 14, 2012, 09:44:40 PM »
That's correct... but by returning from within the try block, you destroy the stack frame and the finally part is not executed.

How can you say he's correct and then assert that what actually happens is entirely the opposite of what he said? BTW he is correct, it's well defined and works perfectly.

czerny

  • Guest
Re: Com port
« Reply #18 on: February 15, 2012, 11:59:29 AM »
One try{} catch{} finally{} construct is tolerably clear. But what about more nested trys?

I often try to code so, that I leave the function in case of an error as soon as possible. A dozen nested if-else statements is not clear at all and a dozen try blocks is even worse.

Code: [Select]
int myfunc(void)
{
  if (errorvalue == func1()) {
    finalize();
    return 0;
  }
  if (errorvalue == func2()) {
    finalize();
    return 0;
  }
  if (errorvalue == func3()) {
    finalize();
    return 0;
  }
  // do what I actually want to do
  return 1;
}

It is sure, that one can not stand this in all cases, but I can often reduce the nesting complexity considerably.

czerny

CommonTater

  • Guest
Re: Com port
« Reply #19 on: February 15, 2012, 04:57:20 PM »
If you wrote your functions to throw an exception...  (shown the Error-X way)
Code: [Select]
int func1( int a)
  { if (a == 0)
       exception(DIV_0);
     return 10 / a; }

... you could do this...

Code: [Select]
try
  { x = func1( y );
     func2( x );
     func3( x );  }
except
   { ReportError(ExcCode,false);
      doCleanup(); }

... and if any of the functions threw an exception the sequence is aborted an error is reported, doCleanup() gets called and execution resumes at the end of the except{} block.
 
There are even cases where you can fix an error...
Code: [Select]
try
  { x = func1(y); }
except
  { y = REPAIR_VALUE;
     x = func1(y); }
func2(x);
func3(x);
... and the user is none the wiser.
 

 
« Last Edit: February 15, 2012, 05:19:01 PM by CommonTater »