Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Bloody
Mar 3, 2013

i thought i knew the difference between checked and unchecked exceptions but then i read this thread and now i am an even more terrible programmer what is this nonsense all about please explain like i am a terrible programmer

Adbot
ADBOT LOVES YOU

cliffy
Apr 12, 2002

Bloody posted:

i thought i knew the difference between checked and unchecked exceptions but then i read this thread and now i am an even more terrible programmer what is this nonsense all about please explain like i am a terrible programmer

um if you check the exception the it's checked and if you don't check it then its unchecked duh

cliffy
Apr 12, 2002

realpostin: is there an opensource project that shows how to architect, implement and handle exceptions well because I've never seen a codebase that does tia

a cyberpunk goose
May 21, 2007

the exception is coming from inside the thread

a cyberpunk goose
May 21, 2007

realtalk; im starting a paid programming internship/job/thing the beginning of next month making tools and demo applications to help our team develop and demo our product. the company has never had someone who isn't total poo poo, try to do any application/software development that isn't just programming ICs to do things. ive been a cj my whole life but always programmin on the side and frequently to aid my cj ways, the job is going to kick my rear end but freshening up on "the know" by skimming programming threads makes me kinda nervous. i was thinking that since these are inhouse tools i should roll with mono or a portable plang+dependencies like python+wxpython (which i've done before and it owns bones), plus the core of my programming knowledge is C++ and i've used boost::python to do crazy poo poo and make importable C++ modules etc.

basically im nervous that my solo cowboy programming methods aren't going to help the company. im good about version control (git or mercurial for life), and i like to make readable commented code with the expectation that some poor bastard will be reading it in 2 years when an obituary includes my name and alcohol poisoning in the same paragraph.

any protips for a fledgling cowboy programmer who needs to make tools other people may have to maintain and use. im still way ahead of most college grads in that i Give A gently caress and dont care what tool it takes to get the job done right, i dunno, yospos help?

edit: like reading the exception discussion here illuminates to me how much competing philosophy there is about how to architect large applications that handle these common design problems well. I generally just try to aim for a service oriented architecture and avoid making dumb code that returns '0' or 'null' as a way of saying something went wrong

a cyberpunk goose
May 21, 2007

im gonna go buy some alcohol

edit:

i forgot to mention that the expectation is that things go well and i end up being "the application programmer" guy, so im trying to know what to expect so that I'm digging a foundation for a good job instead of a grave

a cyberpunk goose fucked around with this message at 02:30 on May 17, 2013

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

tef posted:

oh I forgot shaggar wants checked exceptions and not option types because he's anti union

f# actually makes the programmer discriminate against unions so you'd think he'd be all about it

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

quote:

any protips for a fledgling cowboy programmer who needs to make tools other people may have to maintain and use. im still way ahead of most college grads in that i Give A gently caress and dont care what tool it takes to get the job done right, i dunno, yospos help?

learn to make your code as portable as possible (buy a macbook air)

double sulk
Jul 2, 2010

f# seems like a neat functional language that isn't haskell. i'm so sick of hearing about haskell at work

JewKiller 3000
Nov 28, 2006

by Lowtax
f# is basically ocaml.net, and it's not lazy like haskell is, so you can actually understand the execution flow of your programs

Armourking
Dec 16, 2004

Step off!
Step off!


Doc Block
Apr 15, 2003
Fun Shoe

I'm the one line case statements.

sports
Sep 1, 2012
dont most companies want bad coders because 80% of people are dumb and dont really understand recursion when they read it?

i mean coding simply in an api or something would help people suit their needs because they can read it and anyway computers are really fast and java is really good at memory handling (it isnt) but anyways id rather read a brute force implementation and leave the elegant one for goOD, non p-langs

duTrieux.
Oct 9, 2003


it's petty of companies not to put swear words into their spellcheck dictionaries

CamH
Apr 11, 2008

polpotpi posted:

hi cam how is your imac doing?

im thoroughly enjoying it but waiting for apple to cum out w/ new cinema displays still ty for axing

lol if you
Jun 29, 2004

I am going to remove your penis, in thin slices, like salami, just for starters.

tef posted:

i also found this neat quine in python :3:

quine = 'quine = %r\r\nprint quine %% quine'
print quine % quine

that is the first clever code i've seen in a long time that didn't make me want to deck someone

Deus Rex
Mar 5, 2005

tef posted:

oh I forgot shaggar wants checked exceptions and not option types because he's anti union

could you go into (or link to) a little more detail as to why either/option is superior to checked exceptions? is the problem that in Blub you can (and programmers will) do this to satisfy the compiler:

code:
void foo(int x) throws ButtException {
  ...
}

void bar(int x) {
  try {
    foo(x);
  } catch (ButtException e) {
    // literally do nothnig
  }
}
which is even worse than just letting it bubble up the stack and kill the program? i'm not really familiar with the semantics of checked exceptions because i've never used a language with them

Deus Rex fucked around with this message at 21:23 on May 17, 2013

Juul-Whip
Mar 10, 2008

In the Stanford course on iTunes U, the guy says to enable a breakpoint for all exceptions and to just have that on all the time. Is there any situation where I would not want to do this? Most of the projects I've looked at don't have this breakpoint.

edit: talkin bout Xcode

Juul-Whip fucked around with this message at 00:36 on May 18, 2013

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

THC posted:

In the Stanford course on iTunes U, the guy says to enable a breakpoint for all exceptions and to just have that on all the time. Is there any situation where I would not want to do this? Most of the projects I've looked at don't have this turned on.

edit: talkin bout Xcode

code:
void foo() {
    ...
    throw new ButtException();
}

void main() {
    ...
    try {
        foo();
    } catch (ButtException e) {
        safelyHandleError(e);
    }
    ....
}
you probably aren't interested in having the debugger halt execution whenever an exception occurs.

setting breakpoints on exceptions can be useful when debugging specific problems, of course, but it's not a good general rule.

e: I might be misunderstanding what exactly the lecturer advises. if xcode has functionality that breaks whenever an exception bubbles up to the top level to allow easy stack analysis, that sounds nice & useful.

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice
um no wtf don't use exceptions for normal control flow, test for potential conditions that will cause exceptions and handle them in advance whenever possible

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice
exceptions are a form of non-local goto and should only be used when you have a condition where halting your program and handling it is the only alternative to crashing

they are expensive as gently caress to throw in a lot of languages too

caveat: i'm a .net programmer primarily, i understand python does weird poo poo like using exceptions for control-flow

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice
less terrible coders than me talk about it here:

http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Cold on a Cob posted:

um no wtf don't use exceptions for normal control flow, test for potential conditions that will cause exceptions and handle them in advance whenever possible

it's entirely possible for one section of code to be unable to handle an error condition (e.g. "missing file"), which a higher-level section of code can handle & recover from

this is in fact a canonical use of exceptions

and my point was that you probably don't want to fire a breakpoint every time that happens

Juul-Whip
Mar 10, 2008

It is literally a "break whenever an exception is thrown" breakpoint so if you have an exception somewhere thats crashing your program you can turn this on and it shows you the exact line of code instead of crashing and throwing up gobbledygook. The stanford prof recommends to have it on all the time. You can always disable it if it gets in the way of your regular testing but it seems like a lot of devs don't even know this is a feature. Or maybe it's training wheels for noobs that will gently caress you up in unforeseen ways.

Juul-Whip fucked around with this message at 01:04 on May 18, 2013

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Cold on a Cob posted:

exceptions are a form of non-local goto and should only be used when you have a condition where halting your program and handling it is the only alternative to crashing

they are expensive as gently caress to throw in a lot of languages too

caveat: i'm a .net programmer primarily, i understand python does weird poo poo like using exceptions for control-flow

so, fun python trivia

iteration, e.g. "for foo in bar", uses the iterator interface, defined here. it's pretty simple. two methods: __iter__(), which returns the iterator object itself, and __next__(), which returns the next item to be iterated over. if there is no next item (e.g., you're at the end of a list), raise a StopIteration exception.

this 'makes sense', because the great majority of the time, __next__() will end up returning instead of throwing an exception (for lists longer than 1 item, etc). therefore, running out of items in a list is an exceptional condition.

python is mostly great but it has some really dumb ideas here and there.


THC posted:

It is literally a "break whenever an exception is thrown" breakpoint so if you have an exception somewhere thats crashing your program you can turn this on and it shows you the exact line of code instead of crashing and throwing up gobbledygook. The stanford prof recommends to have it on all the time. You can always disable it if it gets in the way of your regular testing but it seems like a lot of devs don't even know this is a feature.

ok yeah so what I said.

in the Real World, poo poo happens all the time, especially if you're dealing with big distributed systems. things are going to break, and your code is going to have to recover from this. even in a dev context, setting up a 'break on exception' breakpoint like that doesn't really make any sense.

of course, you're doing academic tiny homework assignments, so it makes a lot more sense there. doubly so if you're somehow not able to get stack traces or even line numbers on crashes (????), I guess, though it sounds like something's wrong there.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Cold on a Cob posted:

less terrible coders than me talk about it here:

http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl

I think we're actually agreeing but dear god I hate the page you linked me to

everything looks like it's written by a tvtropes reject

quote:

I HaveThisPattern, I always use return statements to jump out of nested work, it's a natural pattern when using LotsOfShortMethods, goes right along with GuardClauses to bail early.

I mean, what he's saying is totally reasonable, I do the exact same thing, but

uuuugh

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice

PleasingFungus posted:

it's entirely possible for one section of code to be unable to handle an error condition (e.g. "missing file"), which a higher-level section of code can handle & recover from

this is in fact a canonical use of exceptions

and my point was that you probably don't want to fire a breakpoint every time that happens

the higher order code should check for the file in advance and handle it then if its something that will happen often (ie normal code flow)

exceptions are reserved for unexpected error conditions

If it doesn't happen often then it won't hurt to have break on exception turned on and it will help you realize you have a problem if it starts happening suddenly even if your code is robust and recovers

ur wrong on this sorry

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice
if exceptions are indeed used for normal control flow in python the name for them sucks sorry to have to go all shaggar on y'all :shobon:

Bloody
Mar 3, 2013

Cold on a Cob posted:

python sucks sorry :shobon:

Juul-Whip
Mar 10, 2008

Yeah I don't remember seeing a lot of try-catch blocks in the iOS projects I've looked at. It seems that people prefer to guard against them happening in the first place.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Cold on a Cob posted:

the higher order code should check for the file in advance and handle it then if its something that will happen often (ie normal code flow)

exceptions are reserved for unexpected error conditions

If it doesn't happen often then it won't hurt to have break on exception turned on and it will help you realize you have a problem if it starts happening suddenly even if your code is robust and recovers

ur wrong on this sorry

func A: opens a file with a given ID, streams the contents
func B: initializes configuration parameters (using A)

A doesn't know how the file is structured or what it contains, it's just streaming bytes/strings. B doesn't know where the file is, or even if it's a file (as opposed to a database record, a REST request, whatever); it just requests values. this is known as "separation of concerns".

now the file (or whatever) isn't there. A doesn't have the information necessary to recover from a missing file; it throws an error. B catches the error & sets up a default configuration of some kind.

alternately: I'm running a batch operation on a bunch of documents, fetching associated information on each of them across the network. as I go through the information, I find that some of it is corrupt & invalid! the parsing code throws an exception; the iteration code stores the information that there was an error to the database, and moves on. this happens maybe dozens of times across tens of thousands of documents, and it's a normal, expected part of operations.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
tbqh I don't really understand how you can reconcile

Cold on a Cob posted:

exceptions are reserved for unexpected error conditions

with the idea of "catching exceptions".

where exactly do you use exceptions? when do you catch them, if you're only supposed to use exceptions for situations you don't expect? I'm honestly really curious here.

Brain Candy
May 18, 2006

PleasingFungus posted:

now the file (or whatever) isn't there. A doesn't have the information necessary to recover from a missing file; it throws an error. B catches the error & sets up a default configuration of some kind.

its you, you are the coding horror. you check if the file exists in B. an exception is thrown if somebody somehow deletes the file from under you or fills it with spiders.

JewKiller 3000
Nov 28, 2006

by Lowtax
modern programming languages (not java, definitely not any p-lang) make it trivial to declare the return type of a function as an option type, i.e. "ok here's the value OR error", and furthermore the ok/error tag MUST be checked before the potential return value can be extracted

in such languages, the point of an exception is to avoid this behavior, in the few cases where you might want to do so

suppose you're writing a program that does a memory-intensive calculation, but requires 5 function calls to get to the actual work function from main(). the work function is going to allocate a shitton of memory, and there might not be enough, causing it to fail. the work function cannot recover from this error, and neither can any of the other functions in the call stack, except for maybe main(). so, why should i force those 5 functions to check for an error case, when all they're going to do is propagate the error to their caller anyway? moreover, suppose the work function does multiple allocations, and it would be useful for debugging to know which line caused the failure.

when this situation occurs, you can use an exception. otherwise, don't.

JewKiller 3000 fucked around with this message at 03:43 on May 18, 2013

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice

PleasingFungus posted:

tbqh I don't really understand how you can reconcile


with the idea of "catching exceptions".

where exactly do you use exceptions? when do you catch them, if you're only supposed to use exceptions for situations you don't expect? I'm honestly really curious here.

you use them for catching and handling exceptions that are a possibility but do your best to ensure they are never caught in the first place by guarding for them wherever possible

if it is a common scenario in your application that files may not be present or may not validate in some manner, you should be testing for that in the normal flow of your program and dealing with it within the normal flow of your program because it is expected. if that means more thorough testing in each function and passing poo poo back up the stack, so be it.

if it is an uncommon scenario in your application that files may not be present, then by all means use an exception, but treat it like the catastrophic event that it is. catch it and deal with it (eg stop program flow, log the error, have it send an expletive filled sms to your webserver cj's wife)

in c# a good example is using int.TryParse vs int.Parse - you use TryParse when you expect conversion to fail and you use Parse when you never expect it to fail and if it does you want an exception generated.

now, to be fair, i am arguing from authority (essentially, everyone knows non-local goto is bad and should be avoided :smug:). i'm not going to get into all the reasons why non-locally scoped gotos are a bad idea, but if you want to argue that you like being able to shortcut your way up the stack in a non-obvious way when something you expect to happen 5% of the time happens and you don't mind incurring the performance hit for doing so, not to mention making your code less obvious in it's expected operation, have at it but i pity anyone that works on your code base 5 years from now.

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice
heyyyyy look at me i'm arguing on the internet about programming on friday night :smithicide:

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Brain Candy posted:

its you, you are the coding horror. you check if the file exists in B. an exception is thrown if somebody somehow deletes the file from under you or fills it with spiders.

imagine we have an intermediary: A reads the file, B calls A to load a file & parses it, C calls B to get the values & sets the configuration. C doesn't know that a file even exists; it just calls B and says 'get me values'. B has no idea what it's being called for; it's just provided with some kind of resource identifier which it translates into a filename. what should B do when it checks and finds out the file isn't there? what values should it return?

(it should throw an exception)

JewKiller 3000 posted:

modern programming languages (not java, definitely not any p-lang) make it trivial to declare the return type of a function as an option type, i.e. "ok here's the value OR error", and furthermore the ok/error tag MUST be checked before the potential return value can be extracted

:words:

that's nice for people who get to use fun languages over in academia

for people who work in the real world, we have to deal with actual languages

good to know though!

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off

Cold on a Cob posted:

you use them for catching and handling exceptions that are a possibility but do your best to ensure they are never caught in the first place by guarding for them wherever possible

if it is a common scenario in your application that files may not be present or may not validate in some manner, you should be testing for that in the normal flow of your program and dealing with it within the normal flow of your program because it is expected. if that means more thorough testing in each function and passing poo poo back up the stack, so be it.

if it is an uncommon scenario in your application that files may not be present, then by all means use an exception, but treat it like the catastrophic event that it is. catch it and deal with it (eg stop program flow, log the error, have it send an expletive filled sms to your webserver cj's wife)

in c# a good example is using int.TryParse vs int.Parse - you use TryParse when you expect conversion to fail and you use Parse when you never expect it to fail and if it does you want an exception generated.

now, to be fair, i am arguing from authority (essentially, everyone knows non-local goto is bad and should be avoided :smug:). i'm not going to get into all the reasons why non-locally scoped gotos are a bad idea, but if you want to argue that you like being able to shortcut your way up the stack in a non-obvious way when something you expect to happen 5% of the time happens and you don't mind incurring the performance hit for doing so, not to mention making your code less obvious in it's expected operation, have at it but i pity anyone that works on your code base 5 years from now.

hm. I guess the question is something like this:

you have a function. when called, it's unable to return a meaningful result. (a function that's supposed to read from a file doesn't find a file; a function that's supposed to parse formatted data is passed garbage.) what should the function do?

in my philosophy, the function should throw an exception, and that exception should pass upwards until it reaches a function that's capable of dealing with it. ("C" in my earlier example.) I don't think this is particularly controversial, tbqh.

your argument seems to be that exceptions should be used as more verbose quit() calls with nicer error-logging functionality, and only as that. in your scenario, our functions would return error codes instead of actual data in these cases, and every upstream caller would have to pass that on until they reached a function that could deal with the error. that's literally what exceptions were invented to get away from, because it turns control flow into a hideous nightmare, and you don't have any actual argument for returning to it (besides "authority"), so... I guess I'm not convinced!

thanks for articulating all this, though. I was wondering if there was a plausible alternative to the way I use exceptions other than 'error codes'. I guess not!

Cold on a Cob posted:

heyyyyy look at me i'm arguing on the internet about programming on friday night :smithicide:

owned

JewKiller 3000
Nov 28, 2006

by Lowtax

PleasingFungus posted:

that's nice for people who get to use fun languages over in academia

for people who work in the real world, we have to deal with actual languages

good to know though!

i work in industry. what i described can be accomplished with very well understood algorithms from the 1970s. this isn't some academic pie in the sky poo poo, your boss just won't let you use a good programming language, and that's not my fault or my problem!

Adbot
ADBOT LOVES YOU

Cold on a Cob
Feb 6, 2006

i've seen so much, i'm going blind
and i'm brain dead virtually

College Slice

PleasingFungus posted:

hm. I guess the question is something like this:

you have a function. when called, it's unable to return a meaningful result. (a function that's supposed to read from a file doesn't find a file; a function that's supposed to parse formatted data is passed garbage.) what should the function do?

in my philosophy, the function should throw an exception, and that exception should pass upwards until it reaches a function that's capable of dealing with it. ("C" in my earlier example.) I don't think this is particularly controversial, tbqh.

your argument seems to be that exceptions should be used as more verbose quit() calls with nicer error-logging functionality, and only as that. in your scenario, our functions would return error codes instead of actual data in these cases, and every upstream caller would have to pass that on until they reached a function that could deal with the error. that's literally what exceptions were invented to get away from, because it turns control flow into a hideous nightmare, and you don't have any actual argument for returning to it (besides "authority"), so... I guess I'm not convinced!

you're avoiding my main point and a few of my side points.

main point:

if it's something that frequently or commonly happens that your program is expected to deal with and continue, you are dealing with control flow and should not rely on exceptions because they are an expensive non-obvious shortcut up the call stack. for example, if you're dealing with a resource in A that will frequently be unavailable and you need to notify C, it should be documented in the interface for A and B as a return/out value. yes this is more work.

if it's something that rarely happens, it's an exception and you should pass it up the stack, log it, probably halt the process or program depending on the exception type, etc.

side points:
- generating exceptions is an expensive operation. it's wasteful because you're rolling up the call stack and consuming excessive memory and processing power when a simple bool might be more than enough.
- you're hiding functionality. in your example, C knows that A might fail but since B doesn't know, if someone else consumes B they might not be prepared for the fact you're using a non-obvious method of controlling program flow, at least as far as the language is concerned (contrast this with an out parameter in a method signature). i guess checked exceptions might help with this, i'm not a java guy so i don't know much about them. and yeah, maybe python does it differently but like i said, they should at least call it something else then. semantics matter and words have meaning.
- you might accidentally swallow other exceptions (i didn't mention this before but it is a possibility, unless you start generating custom exceptions purely to handle control flow which is more work anyway so you probably wouldn't do that)

  • Locked thread