Tag Info

New answers tagged

0

I think what you are looking for is the finally clause. This will allow you to run some other code, even in the event of an exception. boolean importantFlag = false; //Default value try { importantFlag = getFlag(); } catch (Exception e) { //rethrow exception } finally { //This will run whether or not there is an exception. ...


0

I'm not sure I understand the question, but here's my take on it. In these cases you might want to create a wrapper function that calls the unsafe function safely, and always returns a value. Falling back on a default value if there is an exception. function boolean getFlagSafely(boolean defValue) { try { return getFlag(); } catch(Exception) ...


-2

If you're actually in Java, finalize the variable. boolean foo() { final boolean importantFlag; try { importantFlag = getFlag(); } catch (Exception e) { throw new RuntimeException(e); } return importantFlag; } This will compile and run safely. If you, however, removed the throw statement from the catch block, ...


-1

When dealing with exceptions, it usually pays to have as much code as possible inside the try block. Not just the function itself that could throw, but also everything that uses the return value from the function. In your case, I would write it as: try { boolean importantFlag; importantFlag = getFlag(); doSomeStuff(importantFlag); // more ...


2

Don't ask Us! We're not the ones making business decisions based on the output of your calculations! Having said that, developers need to know how to approach these situations. In my experience, dilemmas like this fall into two broad scenarios: This happens relatively often and business users will see output based on $0.00 sales on a regular basis. This ...


1

My question is: how do you handle the following scenario without null? The way you would handle it on a balance sheet (i.e. it's 0). Maths is a bad example for nulls. Maths has worked without nulls for centuries, and there is never a balance sheet or a general ledger or a kid's maths homework book that says "null". Programming had the problem of what ...


1

@Robert answer requires that you can use a profit margin of 0 if sales are 0, or have the ability at later times to access sales to test if they are 0 before using profit margin. If this is the case, then his answer is acceptable. If however you need to know if the profit margin is 'NaN' at some future processing stage, another approach would be to extend ...


1

Here's how Haskell does it: (not exactly a counter to Lippert's statements since Haskell is not an OO language). WARNING: long winded-answer from a serious Haskell fanboy ahead. Haskell has no null and uses the Maybe data type to represent nullables. Maybe is an algabraic data type defined like this: data Maybe a = Just a | Nothing For those of you ...


5

If sales are zero, profit margin is zero. Zero is a perfectly good result to substitute for the undefined result of a divide by zero calculation in many cases, and it avoids the use of null unless you specifically want to know about the zero sales case.



Top 50 recent answers are included