digitalmars.D.learn - Why writeln can't be converted to nothrow with just catching of
- partypooper (18/18) Feb 21 2022 Do I completely not understand what is `nothrow` or why I can't
- Basile B. (2/20) Feb 21 2022 I believe it's because it can throw ConvException as well ;)
- Basile B. (5/26) Feb 21 2022 However you're totally right to open a discussion, the
- bauss (11/38) Feb 21 2022 What if we could do ex. this:
- Basile B. (2/23) Feb 21 2022 more likely UTFException actually
- partypooper (2/3) Feb 21 2022 Additionaly catching UTF and Conv exceptions doesn't help.
- Basile B. (3/6) Feb 21 2022 Yeah there must be another one then. Something actionnable is the
- partypooper (2/4) Feb 21 2022 What about Mike Parker answer?
- Basile B. (5/9) Feb 21 2022 if nothrow fails that's because things are checked. We could
- Mike Parker (30/32) Feb 21 2022 This has nothing to do with which exceptions types a function
- partypooper (2/33) Feb 21 2022 Thank You for detailed explanation.
- Basile B. (2/20) Feb 21 2022 https://issues.dlang.org/show_bug.cgi?id=22800
- Mike Parker (5/7) Feb 21 2022 D does not have checked exceptions like Java, so the compiler
- partypooper (7/14) Feb 21 2022 Oh, that's explain it.
- Mike Parker (6/11) Feb 21 2022 I'm not sure what you mean. If a function throws an exception, it
Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```
Feb 21 2022
On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```I believe it's because it can throw ConvException as well ;)
Feb 21 2022
On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:However you're totally right to open a discussion, the documentation is innacurate: in https://dlang.org/phobos/std_stdio.html#.writeln just StdioException is mentioned ;)Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```I believe it's because it can throw ConvException as well ;)
Feb 21 2022
On Monday, 21 February 2022 at 10:57:07 UTC, Basile B. wrote:On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:What if we could do ex. this: ```d __traits(possibleExceptions, writeln); ``` Which would give all exceptions a function could possibly throw. That way we could build our try/catch based on that. For nothrow it should return nothing of course, for extern functions that have no source code available it should only return "Exception" of course. Just a thought I just had.On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:However you're totally right to open a discussion, the documentation is innacurate: in https://dlang.org/phobos/std_stdio.html#.writeln just StdioException is mentioned ;)Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```I believe it's because it can throw ConvException as well ;)
Feb 21 2022
On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:more likely UTFException actuallyDo I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```I believe it's because it can throw ConvException as well ;)
Feb 21 2022
On Monday, 21 February 2022 at 10:58:26 UTC, Basile B. wrote:more likely UTFException actuallyAdditionaly catching UTF and Conv exceptions doesn't help.
Feb 21 2022
On Monday, 21 February 2022 at 11:05:42 UTC, partypooper wrote:On Monday, 21 February 2022 at 10:58:26 UTC, Basile B. wrote:Yeah there must be another one then. Something actionnable is the documentation.more likely UTFException actuallyAdditionaly catching UTF and Conv exceptions doesn't help.
Feb 21 2022
On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:Yeah there must be another one then. Something actionnable is the documentation.What about Mike Parker answer?
Feb 21 2022
On Monday, 21 February 2022 at 11:12:38 UTC, partypooper wrote:On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:if nothrow fails that's because things are checked. We could imagine a second flow analysis leading to better error messages. You see, instead of the generic error message you get now. "If it fails, let's see why"Yeah there must be another one then. Something actionnable is the documentation.What about Mike Parker answer?
Feb 21 2022
On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:Yeah there must be another one then. Something actionnable is the documentation.This has nothing to do with which exceptions types a function throws. The compiler doesn't dig into that. You have to catch `Exception`. ```D import std.stdio; class MyException : Exception { this(string msg) { super(msg); } } void throwing(int x) { if(x > 2) throw new MyException("Derp!"); else writeln(x); } void noThrowing() nothrow { try { throwing(10); } catch(MyException me) {} } void main() { noThrowing(); } ``` ``` onlineapp.d(14): Error: function `onlineapp.throwing` is not `nothrow` onlineapp.d(12): Error: `nothrow` function `onlineapp.noThrowing` may throw ```
Feb 21 2022
On Monday, 21 February 2022 at 11:21:52 UTC, Mike Parker wrote:On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:Thank You for detailed explanation.[...]This has nothing to do with which exceptions types a function throws. The compiler doesn't dig into that. You have to catch `Exception`. ```D import std.stdio; class MyException : Exception { this(string msg) { super(msg); } } void throwing(int x) { if(x > 2) throw new MyException("Derp!"); else writeln(x); } void noThrowing() nothrow { try { throwing(10); } catch(MyException me) {} } void main() { noThrowing(); } ``` ``` onlineapp.d(14): Error: function `onlineapp.throwing` is not `nothrow` onlineapp.d(12): Error: `nothrow` function `onlineapp.noThrowing` may throw ```
Feb 21 2022
On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException? This doesn't work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (StdioException) {} } ``` This doest work ```d nothrow void hello() { try { writeln("Hello, World!") } catch (Exception) {} } ```https://issues.dlang.org/show_bug.cgi?id=22800
Feb 21 2022
On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException?D does not have checked exceptions like Java, so the compiler doesn't have anyway to verify that any function you call won't throw a given exception. You have to catch `Exception` to satisfy the `nothrow` requirement.
Feb 21 2022
On Monday, 21 February 2022 at 11:04:46 UTC, Mike Parker wrote:On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:Oh, that's explain it. So with such behavior there is no reason at all to make make function nothrow, if it uses throw functions in its body? And as much as I already know compliler can deduce and automatically adds nothrow to all functions which do not throw exceptions. Right?Do I completely not understand what is `nothrow` or why I can't make function nothrow with just catching StdioException?D does not have checked exceptions like Java, so the compiler doesn't have anyway to verify that any function you call won't throw a given exception. You have to catch `Exception` to satisfy the `nothrow` requirement.
Feb 21 2022
On Monday, 21 February 2022 at 11:11:28 UTC, partypooper wrote:So with such behavior there is no reason at all to make make function nothrow, if it uses throw functions in its body?I'm not sure what you mean. If a function throws an exception, it can't be nothrow.And as much as I already know compliler can deduce and automatically adds nothrow to all functions which do not throw exceptions. Right?Function attribute inference is done in specific circumstances, but not for all functions. See https://dlang.org/spec/function.html#function-attribute-inference.
Feb 21 2022