www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Try/Catch with version condition

reply foo <email example.org> writes:
How would I get this to work:

---
version(foo) {
     try {
}
throw new Exception("This should only be fatal when the version 
is not 'foo'.");
version(foo) {
     } catch (Exception e) {
         import std.stdio;
         writeln(e);
     }
}
---

The only way I could think of to get this to work would be:
---
version(foo) {
     try {
         throw new Exception("This should only be fatal when the 
version is not 'foo'.");
     } catch (Exception e) {
         import std.stdio;
         writeln(e);
     }
} else {
     throw new Exception("This should only be fatal when the 
version is not 'foo'.");
}
---

But then that requires too much code duplication.
What would be the best way to do this?
Apr 09
next sibling parent reply Julian <julian.fondren gmail.com> writes:
On Tuesday, 9 April 2019 at 14:43:54 UTC, foo wrote:
 How would I get this to work:

 ---
 version(foo) {
     try {
 }
 throw new Exception("This should only be fatal when the version 
 is not 'foo'.");
 version(foo) {
     } catch (Exception e) {
         import std.stdio;
         writeln(e);
     }
 }
 ---

 The only way I could think of to get this to work would be:
 ---
 version(foo) {
     try {
         throw new Exception("This should only be fatal when the 
 version is not 'foo'.");
     } catch (Exception e) {
         import std.stdio;
         writeln(e);
     }
 } else {
     throw new Exception("This should only be fatal when the 
 version is not 'foo'.");
 }
 ---

 But then that requires too much code duplication.
 What would be the best way to do this?
Something like this maybe? import std.stdio; void main() { try { throw new Exception("fatal for non-foo versions"); } catch (Exception e) { version(foo) { writeln(e); } else { throw(e); } } writeln("continuing, probably in version foo"); } As used: object.Exception version.d(6): fatal for non-foo versions ---------------- ??:? _Dmain [0x9de15af] continuing, probably in version foo # echo $? 0 vs. object.Exception version.d(6): fatal for non-foo versions ---------------- ??:? _Dmain [0xc4fb9a3] # echo $? 1
Apr 09
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Tuesday, 9 April 2019 at 14:56:03 UTC, Julian wrote:
 Something like this maybe?

   import std.stdio;

   void main() {
       try {
           throw new Exception("fatal for non-foo versions");
       } catch (Exception e) {
           version(foo) {
               writeln(e);
           } else {
               throw(e);
           }
       }
       writeln("continuing, probably in version foo");
   }
Beware though that exception handling is not typically performant. From the spec[1]:
 - Errors are not part of the normal flow of a program. Errors 
 are exceptional, unusual, and unexpected.
 - Because errors are unusual, execution of error handling code 
 is not performance critical.
[1] https://dlang.org/spec/errors.html#the_d_error_handling_solution
Apr 09
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 9 April 2019 at 14:43:54 UTC, foo wrote:
 But then that requires too much code duplication.
 What would be the best way to do this?
I'd simply put the try {} body into a helper function, then make the versions just call that function. void helper() { throw new Exception(""); } version(foo) { try helper(); catch(Exception) {} } else { helper(); } Remember that with D's nested functions, you can define helper functions just about anywhere and containing the same stuff you'd use directly.
Apr 09
next sibling parent reply aliak <something something.com> writes:
On Tuesday, 9 April 2019 at 15:03:03 UTC, Adam D. Ruppe wrote:
 On Tuesday, 9 April 2019 at 14:43:54 UTC, foo wrote:
 But then that requires too much code duplication.
 What would be the best way to do this?
I'd simply put the try {} body into a helper function, then make the versions just call that function. void helper() { throw new Exception(""); } version(foo) { try helper(); catch(Exception) {} } else { helper(); } Remember that with D's nested functions, you can define helper functions just about anywhere and containing the same stuff you'd use directly.
Aye, I'd do the same. And if you want to generalize it then maybe something like: void call(alias func)() { version (foo) { try { func(); } catch (Exception e) { writeln(e); } } else { func(); } } void main() { call!(() { throw new Exception("Boo"); } ); } On a side note, this doesn't work: "call!(() => throw new Exception("Boo"))". Is there any reason throw *has* to be a statement and not an expression?
Apr 09
parent Jacob Carlborg <doob me.com> writes:
On 2019-04-09 19:07, aliak wrote:

 Aye, I'd do the same. And if you want to generalize it then maybe 
 something like:
 
 void call(alias func)() {
      version (foo) {
          try {
              func();
          } catch (Exception e) {
              writeln(e);
          }
      } else {
          func();
      }
 }
 
 void main() {
      call!(() { throw new Exception("Boo"); } );
 }
 
 On a side note, this doesn't work: "call!(() => throw new 
 Exception("Boo"))". Is there any reason throw *has* to be a statement 
 and not an expression?
Here's an alternative, although "throw" is duplicated: void call(lazy Exception func) { version (foo) { try { throw func(); } catch (Exception e) { writeln(e); } } else { throw func(); } } void main() { call(new Exception("Boo")); } -- /Jacob Carlborg
Apr 09
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 4/9/2019 8:03 AM, Adam D. Ruppe wrote:
 I'd simply put the try {} body into a helper function, then make the versions 
 just call that function.
I shoulda read this before posting the identical solution!
Apr 09
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 9 April 2019 at 23:29:31 UTC, Walter Bright wrote:
 I shoulda read this before posting the identical solution!
Hah! Well, the nested functions are one of my favorite day-to-day tool in D, so they deserve double mention :)
Apr 09
parent Walter Bright <newshound2 digitalmars.com> writes:
On 4/9/2019 4:39 PM, Adam D. Ruppe wrote:
 On Tuesday, 9 April 2019 at 23:29:31 UTC, Walter Bright wrote:
 I shoulda read this before posting the identical solution!
Hah! Well, the nested functions are one of my favorite day-to-day tool in D, so they deserve double mention :)
I've programmed for decades without them, so I don't normally think in terms of them. I keep finding more and more uses for them.
Apr 09
prev sibling next sibling parent Sebastiaan Koppe <mail skoppe.eu> writes:
On Tuesday, 9 April 2019 at 14:43:54 UTC, foo wrote:
 But then that requires too much code duplication.
 What would be the best way to do this?
I like this approach: --- import std.stdio; void tryIt(T)(lazy T t) { version (foo) try { t(); } catch (Exception e) { writeln(e); } else t(); } void whatever() { throw new Exception("Hello"); } void main() { whatever.tryIt(); } ---
Apr 09
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 4/9/2019 7:43 AM, foo wrote:
 How would I get this to work:
Nested functions: void bar() { throw new Exception("This should only be fatal when the version is not 'foo'."); } version (foo) { try { bar(); } catch (Exception e) { import std.stdio; writeln(e); } } else bar();
Apr 09