Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Who needs exceptions?

Name: Anonymous 2012-07-04 20:46


//in some class or namespace:
typedef unsigned int error_code;
const error_code out_of_mem = 0x01,
  niggers = 0x02,
  jews = 0x04;

error_code f()
{
  error_code ret = 0;
  //do some stuff...
  //suddenly niggers
  ret |= niggers;
  //but it's not a fatal error so we will continue execution
  //...
  if(some_pointer == nullptr)
  {
    ret |= out_of_mem;
    goto cleanup;
  }
  //and so on...
cleanup:
  delete some_pointer;
  //etc...

  return ret;
}

void implementation()
{
  error_code f = f();
  if( f & out_of_mem )
    //handle error
    printf("%s\n", "allocation failure");

  if( f & niggers )
    pool.close();
}


The main point of exceptions is to separate error detection from error handling. This accomplishes that. If you need the advanced features of exceptions, take a good look at your design and, more importantly, your life.

Name: Anonymous 2012-07-04 20:58

goto cleanup;
WHAT ARE YOU DOING!?

Name: Anonymous 2012-07-04 21:11

wow dude you have made a hatchet job of this. learn to write proper code. no goto's, no custom error codes unless your codebase is the size of my dick (which is huge)

Name: Anonymous 2012-07-04 21:20

NO EXCEPTIONS

Name: Anonymous 2012-07-04 21:20

>>3
The goto is necessary to avoid resource/file leaks. Note that you don't get this with exceptions unless you use smart pointers. And smart pointers consume a massive class for a fucking pointer of all things, where a couple simple jump instructions would have sufficed.

Name: Anonymous 2012-07-04 21:34

>>3
Yeah, but your program now has run into a serious problem: it's run out of available memory, but it may not stop trying to do things that it would normally do.  Continuing as things are could create problematic states elsewhere, jeopardizing stability or configuration, and you wouldn't notice until the damage became significant or unless you checked an error log.

Sure, you can just quit the program here if the problem detected is that serious; I'm not saying that.  But that still means having to put redundant code that handles the affairs of reporting back to the user/developer what went wrong and where.

Name: Anonymous 2012-07-04 21:35

>>6
Sorry, >>3, that was supposed to be >>5.

Name: Anonymous 2012-07-04 23:13

>>6
Exceptions don't deal with (no available memory) any better than this does.

And I don't see how comparing integers is redundant code. It's something so quick and trivial that it doesn't take a second's more thought than throw some ugly exception. And it's more efficient. Prove me wrong.

Name: Anonymous 2012-07-04 23:32

>>8
I said error logging/reporting, not the conditional.  If the same error is encountered in multiple places, everywhere that can be a problem has to have a copy of that code.  It's likely that the same kind of error isn't handled radically different in different parts of the code and anything that's recoverable shouldn't need to be an exception-worthy issue anyway.  Showstoppers, but not general cleanup, like what the OP could do by just failing early and gracefully.  The goto is superfluous.

If we're ruling out exceptions because they do not deal with out of memory conditions any better than goto'ing somewhere else, then the OP example can not be considered a valid example of an argument for branching instead of exceptions and I request a more suitable one.  In this state, the program is still doing things and can potentially enter half-states where one part begins misbehaving while the other part gives the impression that it's working correctly.  That does depend on how you what the problem is and how you can handle it, admittedly, but it's not usually good.

Name: Anonymous 2012-07-05 0:37

I need exceptions because I HATE GLOBAL VARIABLES THEY ARE THE DEVIL.

Name: Anonymous 2012-07-05 1:13

implying C++'s runtime isn't already using exceptions en mas

are you an queer!!


Name: Anonymous 2012-07-05 2:04

>>11
/polecat kebabs/

Name: Anonymous 2012-07-05 2:11

>>11

I always compile with -fno-exceptions -fno-rtti -flto -ffunction-sections -fdata-sections -Os and link with -Wl,--gc-sections -Wl,-flto in addition.

Fuck your C++ runtime.

Name: Anonymous 2012-07-05 16:51

>>11
Wrong.

Name: Anonymous 2012-07-05 20:52

蔉س倲餢椡䌓䕴爄色㡳ኀ䙣㈢眰䠥ᒂ㒙恄㘁䥃卖⊓挘摴祆荱艓ʑظ舂艁䠇挴⍆鉷㜉ᢐሒ晔萙祗㙃ᥐ㉣噂䙶吧ᤰ䡐閅剅瘢杓䊃瑓畁鍠䡀⠸ɳ३阡衴▄褡∡灘ᜀ閄㕔舷Їؠ㘶ࡂ焀㎓楨的ᑥ唤頔椱❕㎔╲᜕ធ⎖霡萉゗ٲ鎁䡥戈镓覙畢薃逑ၥ掖別䠇ć南膖⌔㤘瀲ᠷ脱䞙ሀ∆夂儹䍉腶✥ᔒ㈓ᎂ畸⍨ȡ砣鉑⡨䆈࠹㖐皒琨獇甉搄☙硱蔑⍸㉲陇暁ဲ锹ވ先䜹理܄ጣ蕤皅ᔠ饓ᦔ酂ږ邈呕㐕猕聣ᘑ猣畒鉗ܖ镨褢邁琔䌓ޒ瀇⡹⌢癢ܐ葂㥆葧按⤅礀ঔ慸ᤙ↔嘷晷呷研ㄓ䕀䆉ᘔ饗榙椳捶☑䐀楂┦蝶ၑĨ桐ᝒ℆圳ᅷ䚖΄㜹祑砡頇頱䥇᥹散᜹杶匕塩傀刅㙀锔㡵ቀ蒉䄕袓⍷⚄⍇䄔ॉᠦ値䈢桓蜄᠄ࠆ扱鑇ᝀ扅ᔹ࠹ቐ夣v厂ॵ㝰㔤员傇憅梁葱ᡐ煘ጀᥦ䉈ㄡ栲䦕陖ղ搘❑ؖ档睲顁椱ᐢㆉ鐄❥䈲灹甗┉䝦ᘱ䀒䒑化⌲⅓

Name: Anonymous 2012-07-05 21:02

throw MY ANUS

Name: Anonymous 2012-07-05 22:14

>>16
I'm not catching THAT.

Name: bampu pantsu 2012-07-06 4:57

bampu pantsu

Name: Anonymous 2012-07-06 13:59

葙杰چͳᄸ炓ȑ琈ᐢᔅ褖႐㖃☡̣Y䄤✖芔䉥㒅匁䂕≢恶坷⌈ᙸ撇ℳᢗᕄ瀗茓灖㜷褢䄡有ȩɢ錴⡁茆蒕অ礂䌀馘㤩ᘔ劑ጨ㈐鍔玁ၳ邗ₔ⅐❈荷䅙ƕ搙ġᄣ邗`✷餈靡᠉蜉䞕䐓║甤䔠猉艩⒄鍂喐䦉皕ၲ饥甕煘㤩㘅题䙒ԃ蝂䘰ࠣ蚙䥔℗祁奠礡牙॔偘噢逗㕡莘先㌱ԧᢆ䦑螒ᅣ☦ቨၖ䡠a虙ॱ餗Ɨ❵萤㦑瘀襷㄂ևၗ硠⌗晆ဨ䈹䝂靸䑐礂鐡靃茙⚁唀靡ᤕぃ偶銄⤦Ă衰捗喐茂嚄ɤখ㜧霰᝸垙衶ࡸ䚁梉倘玗嘳夡顱晙㞃腖敹᎖阧ᢇ䤄荱塐ᑓጶ镨特⑹⥰馁ă昒⥠莙霰圁㍓餁䍩C✳葄⁴㡡ゅ特朑ܹ靲恢᠗嘁'怱荢蔲⡥␢晡鈹咃㍤蔂杦㚇栦瀔ច戡䞃䠩Ʌ脠♐䉃覔咐猠敇㔰㚇晃衁䍹茈ፕ؅蔧瑄з㡇Ւ儵ጣ㡂琳㈤♦7ᤥ炕䁕ᆅဃ敶䆃ܥ儓ဴ覓ᕁ䄁挀␉瀆㘸≵ᥧ梆芙琳㑷卹ܹ ⤤墖ٸㅠᤦِ熆爇鍷ࠇ㑥鉄

Name: Anonymous 2012-07-06 14:52

Who needs exceptions?
I know it's not really /prog/'s style, but I will take a shot at actually answering the question asked by >>1, since it's a programming question.

No one "needs" exceptions, but they are very convenient and efficient in certain situations.  A common example is when you write deeply recursive code, as in any type of parser.  When you encounter a parse error, your options are to either return some failure code up through dozens of levels of call stack -- meaning that you'll have to check the return value at every level, as well (inefficient) -- or to just throw an exception immediately when the error is encountered and catch at the top level.

Another reason to use them is that they make code more readable.  They separate the actual "functional" code from the error handling code.

Without exceptions:

  error_code e = init_driver();

  if (e == error_device_not_ready)
    ...
  else if (e == error_device_already_initialized)
    ...
  else if (e == ...)
    ...

  e = get_device_status(status);

  if (e == error_device_not_ready)
    ...
  else if (e == error_unsupported)
    ...
  else if (e == ...)
    ...

  e = write_data_to_device(data);

  if (e == error_device_not_ready)
    ...
  else if (e == error_wrong_data_format)
    ...
  else if (e == ...)
    ...


With exceptions:

  try
  {
    init_driver();
    get_device_status(status);
    write_data_to_device(data);
  }
  catch (error_device_not_ready)
  {
    ...
  }
  catch (error_device_already_initialized)
  {
    ...
  }
  catch (error_unsupported)
  {
    ...
  }
  catch (error_wrong_data_format)
  {
    ...
  }

Name: Anonymous 2012-07-07 1:16

are error codes faster than exceptions?

Name: Anonymous 2012-07-07 1:31

>>21
Implementation dependent.

Name: Anonymous 2012-07-07 8:39

They can be useful for things like event handlers, where you don't want an event handler to take your program down,  so you can wrap the whole notification in a try/catch.

But I don't agree with functions from a language's standard library throwing stuff from simple functions.

HURR DURR OPEN throws FILENOTFOUNDEXCEPTION.  That shit can fuck off.

Name: Anonymous 2012-07-07 16:05

>>1
Tsk.

Name: Anonymous 2012-07-07 23:45

>>22
Wut. How, in any implementation, can returning an integer be less efficient than exceptions? Have you seen all the instructions a single throw and catch pair introduce?

Exceptions are fucking unnecessary, harmful shit.

Name: Anonymous 2012-07-09 0:46

>Who needs exceptions?
Java programmers

Name: Anonymous 2012-07-09 1:09

>>26
Java programmers are the exceptions that I wish to throw.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-07-09 1:14

http://lyngvig.org/a-proposal-for-exception-handling-in-c.ashx

Using the CPU's carry flag for returning boolean values and signaling errors: It's been over 30 years since the 8086 was released, and we are finally seeing some of these established efficient customs of asm propagating to the first level of high-level languages. Nothing can beat the 1 + 2n bytes needed to return a boolean like this (1 byte to set, 2 bytes to test+jump per callsite); the best C compilers will generate a 2 + 4n (2 bytes to set, 2 bytes to test + 2 bytes to jump per callsite).

See also: custom calling conventions, return address retargeting, cross-function register allocation, etc.

Name: Anonymous 2012-07-09 1:39

>>25

setjmp and longjmp.

Name: Anonymous 2012-07-09 13:46

Amen.

Name: Anonymous 2012-07-22 0:19

bampu pantsu

Name: Anonymous 2012-07-22 0:28

>>28
Wouldn't that tie C up to a specific architecture and therefore be stupid as all hell?

Name: Anonymous 2012-07-22 3:24

>>32
No, UNIX retard. On systems which don't have a free 1-bit register the compiler will be able to just use another register or memory.

Name: Anonymous 2012-07-22 3:31

>>33
what a waste of air and doubles.

Name: Anonymous 2012-07-22 3:35

if you have an exception it occurs once,not millions of times, so performance of asm is irrelevant.

Name: Anonymous 2012-07-22 3:50

>>35
The issue is more about machine code bloat, which has a slight impact on performance due to L1 cache limitations.

Name: Anonymous 2012-07-22 6:06

>>36
If your exception system has constant overhead, something's wrong.

Name: Anonymous 2012-07-22 8:50

>>37
You can't VBR an exception system.

Name: Anonymous 2012-07-23 6:53

Only morons complain about gotos.  They are probably the same kind of people who advocate confiscation of firearms from all civilians who possess any.

Don't change these.
Name: Email:
Entire Thread Thread List