|
The code in a finally block gets executed whether an exception is thrown or not. This does neither apply to regular blocks nor to catch blocks, so this is important.
|
# ? Mar 2, 2008 06:24 |
|
|
# ? May 15, 2024 03:42 |
|
dougdrums posted:So I was (not now, earlier) and programming and thinking about the finally statement. Is there any god drat reason for the finally statement? The only situation I see using finally in is if I didn't want to put a catch statement after a try which is a horrible practice anyways. And even if I did want to do that, couldn't I just use catch { } instead? This construct exists in Java too (does C++ have it?), and any introductory text on either language will tell you exactly what finally is for. It's not a replacement for a catch. http://neptune.netcomp.monash.edu.au/JavaHelp/howto/try_catch_finally.htm#whyFinally
|
# ? Mar 2, 2008 06:32 |
|
I didn't think about catch throwing the exception. When I took java my instructor straight out told us he had no clue what it was for. Thanks.
|
# ? Mar 2, 2008 06:54 |
|
csammis posted:This construct exists in Java too (does C++ have it?), and any introductory text on either language will tell you exactly what finally is for. It's not a replacement for a catch. C++ doesn't have "finally" because you should be using RAII instead.
|
# ? Mar 2, 2008 06:57 |
|
dougdrums posted:I didn't think about catch throwing the exception. When I took java my instructor straight out told us he had no clue what it was for. Thanks. finally has nothing to do with catch throwing an exception. You should brush up on exceptions and how they're used, as it seems you don't understand them.
|
# ? Mar 2, 2008 06:57 |
|
dougdrums posted:So I was (not now, earlier) and programming and thinking about the finally statement. Is there any god drat reason for the finally statement? The only situation I see using finally in is if I didn't want to put a catch statement after a try which is a horrible practice anyways. And even if I did want to do that, couldn't I just use catch { } instead? code:
|
# ? Mar 2, 2008 07:00 |
|
dougdrums posted:I didn't think about catch throwing the exception. When I took java my instructor straight out told us he had no clue what it was for. Thanks. Then the Java you were taught is crippled, because finally is fundamental. Pseudo-code, since it's been so long since I worked in Java: code:
Without finally, you'd have to write this in one of two ways: code:
code:
Actually, it just occurred to me that neither of these will work if you don't want to catch IOException - if your routine is supposed to throw exceptions to the parent, there's absolutely no way to safely clean up without the finally statement: code:
code:
|
# ? Mar 2, 2008 07:12 |
|
floWenoL posted:You should brush up on exceptions and how they're used, as it seems you don't understand them. No kidding, that's why I asked. I started off with assembly and C so this is new to me. Chill with the programming e-peen. JoeNotCharles posted:Then the Java you were taught is crippled, because finally is fundamental. This made it really clear to me, thanks. I can think of a few places where I should have used it before. The guy that taught it was sorta in the same boat I was in. He does more low level programming and got stuck teaching java. I ended up learning java from my horrible textbook anyways. dougdrums fucked around with this message at 07:45 on Mar 2, 2008 |
# ? Mar 2, 2008 07:43 |
|
csammis posted:An empty list isn't null in Java, it's just empty. size() == 0.
|
# ? Mar 2, 2008 10:52 |
|
It is also faster if your standard library implementor decides to implement .size() in O(n) csammis, C++/CLI has finally, by the way.
|
# ? Mar 2, 2008 12:14 |
|
dougdrums posted:So I was (not now, earlier) and programming and thinking about the finally statement. Is there any god drat reason for the finally statement? The only situation I see using finally in is if I didn't want to put a catch statement after a try which is a horrible practice anyways. And even if I did want to do that, couldn't I just use catch { } instead? It's used in Python for the same reasons other people stated: code:
Newer versions of Python have a nicer syntax for that: code:
|
# ? Mar 2, 2008 17:51 |
|
Milde posted:No need for the explicitly finally/close, the with statement sets it up so f.__exit__ is always called, which simply calls close() (and this __enter__/__exit__ protocol can be adapted to other kinds of classes, like database cursors, thread locks, etc.) That's kind of similar to RAII. I like Python more and more every day
|
# ? Mar 2, 2008 19:07 |
|
Milde posted:Newer versions of Python have a nicer syntax for that: For posterity, C# has this too in the using keyword: code:
|
# ? Mar 2, 2008 19:10 |
|
Nice you see that you guys are almost catching up to C++, and your syntax is only minimally clumsier too! Edit: They should codename C# 4.0 "playing catchup"
|
# ? Mar 2, 2008 23:17 |
|
I'm having to do some flow control based upon exceptions that come back in Java and am wondering if there's anything that can do the following more cleanly:code:
code:
|
# ? Mar 3, 2008 03:12 |
|
I'd probably write a helper function:code:
|
# ? Mar 3, 2008 04:02 |
|
I have a bit of code that I'm working on, and it has a use of '&' that I've never seen before. quote:switch(rand()&3) I've seen rand() % 3 used a million times before, but never the &. It's still giving me random numbers but it seems like it's doing something a bit different. What does this do?
|
# ? Mar 3, 2008 05:48 |
|
Femtosecond posted:I have a bit of code that I'm working on, and it has a use of '&' that I've never seen before. 3 is 0011 in binary. & is the bit-wise and operator. Basically, its giving you a random number between 0 and 3 (inclusive). It is equivalent to "% 4"
|
# ? Mar 3, 2008 06:14 |
|
YardKGnome posted:3 is 0011 in binary. & is the bit-wise and operator. Basically, its giving you a random number between 0 and 3 (inclusive). It is equivalent to "% 4" That seems like a bad idea. Somebody is going to want to add another case, change the 3 to a 4, and now you have a bug.
|
# ? Mar 3, 2008 16:32 |
|
very posted:That seems like a bad idea. Somebody is going to want to add another case, change the 3 to a 4, and now you have a bug. Yup. Seems like a pretty bad idea in general.
|
# ? Mar 4, 2008 04:23 |
|
I found a regular expression that seems to work when I test it out using this page: http://tools.netshiftmedia.com/regexlibrary/ But when I try to use it in my script, I'm getting an error. What am I doing wrong? Here is my code: code:
edit: long line broken up to prevent page breaking, the actual code does not have the line broken up
|
# ? Mar 4, 2008 07:45 |
|
You need to surround the regex with slashes, like you would in Perl. Also why in god's name do you have a regex like that? I think when you have to worry about table breaking with a regex, you might want to look into writing a grammar to do whatever it is you're doing (possibly hard in Javascript unless there's a library for it). The benefit of a grammar is that it isn't write-only code.
|
# ? Mar 4, 2008 09:07 |
|
I don't get it. What about that regex is unreadable? edit: This is for an obfuscation contest, right?
|
# ? Mar 4, 2008 10:19 |
|
This is a bit of a weird question but what programming languages use :: (double colon) as part of their syntax and for what? Info on any language would be appreciated, particularly if the syntax has different uses. (I've tried searching google but then google doesn't like non-alphanumeric searches) The only ones I know are C++ and PHP, C++ seems to use it for member functions and scope resolution. I don't understand how the compiler knows the difference -- how is that? PHP uses it for public static functions Are member functions the same as public static functions? Update (the following day) sorry, I can't post an explanation yet but I will soon. I know this sounds weird but there's a good reason. I'll post in this thread with an update later (I suppose if you want to be notified send me a PM or an email on what-the-hell-is-the-double-colon-about@holloway.co.nz ) N.Z.'s Champion fucked around with this message at 00:17 on Mar 5, 2008 |
# ? Mar 4, 2008 11:08 |
|
Off the top of my head... Miranda, Haskell, and that entire family of languages use it as the has-type operator. The Caml/OCaml family of languages use it as a cons, if I remember correctly. Perl and Ruby use it as a namespacing operator; probably lots of others in the nuevo-dynamic-language family.have a similar use. Several versions of make use the double-colon operator to construct prerequisite-dependent rules. And you have got to explain why you're asking this question.
|
# ? Mar 4, 2008 11:43 |
|
Can we get The book thread sticked? This is the second or third time we've had one now and it would be nice to keep one. Aside: I'm tempted to write a FAQ but really the only frequent question is "WHat Language should I learn?" Edit: I'm sure I meant to post this in the other sticked thread tef fucked around with this message at 18:06 on Mar 4, 2008 |
# ? Mar 4, 2008 13:02 |
|
N.Z.'s Champion posted:This is a bit of a weird question but what programming languages use :: (double colon) as part of their syntax and for what? Info on any language would be appreciated, particularly if the syntax has different uses. http://merd.sourceforge.net/pixel/language-study/syntax-across-languages.html
|
# ? Mar 4, 2008 13:05 |
|
tef posted:Aside: I'm tempted to write a FAQ but really the only frequent question is "WHat Language should I learn?" And the answer is "don't bother asking because you won't listen anyway"
|
# ? Mar 4, 2008 15:50 |
|
csammis posted:And the answer is "don't bother asking because you won't listen anyway" "Who needs a project to learn a language when I can post a thread to ask moot questions" tef fucked around with this message at 17:33 on Mar 4, 2008 |
# ? Mar 4, 2008 17:26 |
|
Avenging Dentist posted:You need to surround the regex with slashes, like you would in Perl. Why waste time writing a complicated function when you can just find a regex via google and plop it in? edit: by the way, adding slashes doesn't fix it either. I'm now getting an "unterminated regular expression literal" error. I've now tried 3 or 4 different regex's I've found with Google, and they either give me a syntax error, or they parse fine, but return false every time. this is starting to piss me off. nbv4 fucked around with this message at 18:43 on Mar 4, 2008 |
# ? Mar 4, 2008 18:03 |
|
nbv4 posted:edit: by the way, adding backslashes doesn't fix it either. Avenging Dentist didn't suggest adding backslashes. He suggested slashes, aka forward slashes.
|
# ? Mar 4, 2008 18:23 |
|
N.Z.'s Champion posted:This is a bit of a weird question but what programming languages use :: (double colon) as part of their syntax and for what? Info on any language would be appreciated, particularly if the syntax has different uses. Fortran 90/95 (and maybe 2003, but I'm not as familiar with it) uses it in variable declaration. code:
|
# ? Mar 4, 2008 18:42 |
|
6174 posted:Avenging Dentist didn't suggest adding backslashes. He suggested slashes, aka forward slashes. gah, I have a habit of calling either back slashes or front slashes "backslashes". I tried adding front slashes and it gave me the "unterminated regular expression literal" error. Backslashes gave me a syntax error.
|
# ? Mar 4, 2008 18:42 |
|
::Visual Basic:: How can i control the rate at which i change the properties of labels.. I'm trying to mimic a pump transferring water from one tank to another.. Each tank is made up of 10 tables. At the form load, the right tank's labels all the visible = true and the left tank has visible = false... I want to dynamically change the right tank's labels to visible = false and the left to true at X rate.. The problem I'm having is controlling the rate at which this happens... There are meant to be 5 different rates that the user can set.. rate 1 means it takes 10 seconds to change all the properties and rate 5, 2 seconds. The other problem I'm having is that at the moment i have to directly state each label to change its properties.. Is there anyway i can say like code:
Thanks in advance.
|
# ? Mar 4, 2008 19:46 |
|
karuna posted:::Visual Basic:: If you want something to take a certain amount of time, you can use a timer. Timers have a tick function that gets called every specific amount of time, and you can turn them on and off and specify the amount of time etc. In order to be able to refer to your controls by number, you need to make a control array. I don't know the specifics of how to implement these in VB, and it probably depends on what version of VB you are using, but at least you know what to google for now.
|
# ? Mar 4, 2008 19:59 |
|
Just did a quick search there and it seems control arrays are no longer supported, I'm using visual basic 2008 express edition.. Would you know of any other way i can refer to the labels with a variable? Cheers
|
# ? Mar 4, 2008 20:10 |
|
karuna posted:Just did a quick search there and it seems control arrays are no longer supported, I'm using visual basic 2008 express edition.. VB 2008 is .NET, please refer to the .NET Questions Megathread. You may be able to use reflection to access the labels.
|
# ? Mar 4, 2008 20:16 |
|
nbv4 posted:gah, I have a habit of calling either back slashes or front slashes "backslashes". I tried adding front slashes and it gave me the "unterminated regular expression literal" error. Backslashes gave me a syntax error. It works fine for me. You're doing the following right? code:
Avenging Dentist fucked around with this message at 20:59 on Mar 4, 2008 |
# ? Mar 4, 2008 20:35 |
|
Avenging Dentist posted:It works fine for me. You're doing the following right? this is the exact error I get. And here is the top few lines of my code: code:
|
# ? Mar 4, 2008 20:54 |
|
|
# ? May 15, 2024 03:42 |
|
Get rid of the newlines in the regex (obviously don't do this in the post ).
|
# ? Mar 4, 2008 20:56 |