digitalmars.D.learn - throws Exception in method
- amehat (11/11) May 08 2014 Hello everyone,
- John Colvin (3/14) May 08 2014 All functions/methods can throw in D, unless they are marked
- bearophile (7/8) May 08 2014 D doesn't have that Java syntax, because it was widely regarded
- Jonathan M Davis via Digitalmars-d-learn (20/31) May 08 2014 On Thu, 08 May 2014 09:15:13 +0000
- amehat (19/70) May 08 2014 My English might not be very good and I'm not sure I understand.
- John Colvin (16/96) May 08 2014 Exceptions are runtime-only (or ctfe (compile time function
- amehat (4/114) May 08 2014 Okay.
- monarch_dodra (9/12) May 08 2014 Keep in mind that D also has the concept of "Error". Both
- amehat (4/16) May 08 2014 I do not know the mistakes, at least not as you speak. I just
- H. S. Teoh via Digitalmars-d-learn (25/41) May 08 2014 What he meant, is that in D there are two kinds of throwables:
Hello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank you
May 08 2014
On Thursday, 8 May 2014 at 09:15:16 UTC, amehat wrote:Hello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank youAll functions/methods can throw in D, unless they are marked nothrow.
May 08 2014
amehat:What is the proper behavior for this D?D doesn't have that Java syntax, because it was widely regarded as a Java design mistake. So in D omit the throws part. If your function tree doesn't throw exceptions (but it can throw errors) add a "nothrow". Bye, bearophile
May 08 2014
On Thu, 08 May 2014 09:15:13 +0000 amehat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Hello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank youAt this point, the programming community at large seems to have decided that while checked exceptions seem like a good idea, they're ultimately a bad one. http://www.artima.com/intv/handcuffs.html At this point, Java is the only language I'm aware of which has checked exceptions (though there may be a few others somewhere), and newer languages have learned from Java's mistake and chosen not to have them. What D has instead is the attribute nothrow. Any function marked with nothrow cannot throw an exception. e.g. auto func(int bar) nothrow {...} It's similar to C++11's noexcept except that it's checked at compile time (like Java's checked exceptions), whereas noexcept introduces a runtime check. If a function is not marked with nothrow, then the only ways to know what it can throw are to read the documentation (which may or may not say) or to read the code. There are obviously downsides to that in comparison to checked exceptions, but the consensus at this point is that it's ultimately better. - Jonathan M Davis
May 08 2014
On Thursday, 8 May 2014 at 10:14:27 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:On Thu, 08 May 2014 09:15:13 +0000 amehat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:My English might not be very good and I'm not sure I understand. If I understand what you say, D for all methods (and functions) can raise exceptions, unless it has nothrow. And if I still includes exceptions that are thrown are at the time of compilation. So I can not write: public void testMe () throws MyException {} However, if I write this and my method throws an exception, it will take place at compile time: public void testMe () {} And if do not want an exception thrown, I should write: public void testMe () : nothrow {} or perhaps : public void testMe () pure nothrow safe {} Is that correct? PS: Thanks for the article on the interveiw Anders Hejlsberg, itHello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank youAt this point, the programming community at large seems to have decided that while checked exceptions seem like a good idea, they're ultimately a bad one. This article has a good explanation from one of the creators of http://www.artima.com/intv/handcuffs.html At this point, Java is the only language I'm aware of which has checked exceptions (though there may be a few others somewhere), and newer languages have learned from Java's mistake and chosen not to have them. What D has instead is the attribute nothrow. Any function marked with nothrow cannot throw an exception. e.g. auto func(int bar) nothrow {...} It's similar to C++11's noexcept except that it's checked at compile time (like Java's checked exceptions), whereas noexcept introduces a runtime check. If a function is not marked with nothrow, then the only ways to know what it can throw are to read the documentation (which may or may not say) or to read the code. There are obviously downsides to that in comparison to checked exceptions, but the consensus at this point is that it's ultimately better. - Jonathan M Davis
May 08 2014
On Thursday, 8 May 2014 at 12:00:40 UTC, amehat wrote:On Thursday, 8 May 2014 at 10:14:27 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:Exceptions are runtime-only (or ctfe (compile time function execution), but that's not relevant here). functions that are not marked nothrow can, at any point in the body of the function, exit via an exception. Functions marked nothrow cannot exit via an exception; the compiler must be able to prove that all exceptions that could be thrown are caught, e.g. void foo() nothrow { try { throw new Exception(""); } catch(Exception e){} } is fine.On Thu, 08 May 2014 09:15:13 +0000 amehat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:My English might not be very good and I'm not sure I understand. If I understand what you say, D for all methods (and functions) can raise exceptions, unless it has nothrow. And if I still includes exceptions that are thrown are at the time of compilation. So I can not write: public void testMe () throws MyException {} However, if I write this and my method throws an exception, it will take place at compile time: public void testMe () {} And if do not want an exception thrown, I should write: public void testMe () : nothrow {} or perhaps : public void testMe () pure nothrow safe {} Is that correct? PS: Thanks for the article on the interveiw Anders Hejlsberg, it enlightens me a little more about how exceptions sen D (andHello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank youAt this point, the programming community at large seems to have decided that while checked exceptions seem like a good idea, they're ultimately a bad one. This article has a good explanation from one of the creators http://www.artima.com/intv/handcuffs.html At this point, Java is the only language I'm aware of which has checked exceptions (though there may be a few others somewhere), and newer languages have learned from Java's mistake and chosen not to have them. What D has instead is the attribute nothrow. Any function marked with nothrow cannot throw an exception. e.g. auto func(int bar) nothrow {...} It's similar to C++11's noexcept except that it's checked at compile time (like Java's checked exceptions), whereas noexcept introduces a runtime check. If a function is not marked with nothrow, then the only ways to know what it can throw are to read the documentation (which may or may not say) or to read the code. There are obviously downsides to that in comparison to checked exceptions, but the consensus at this point is that it's ultimately better. - Jonathan M Davis
May 08 2014
On Thursday, 8 May 2014 at 12:27:55 UTC, John Colvin wrote:On Thursday, 8 May 2014 at 12:00:40 UTC, amehat wrote:Okay. Thank you for these explanations, I understand a little better the exceptions D.On Thursday, 8 May 2014 at 10:14:27 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:Exceptions are runtime-only (or ctfe (compile time function execution), but that's not relevant here). functions that are not marked nothrow can, at any point in the body of the function, exit via an exception. Functions marked nothrow cannot exit via an exception; the compiler must be able to prove that all exceptions that could be thrown are caught, e.g. void foo() nothrow { try { throw new Exception(""); } catch(Exception e){} } is fine.On Thu, 08 May 2014 09:15:13 +0000 amehat via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:My English might not be very good and I'm not sure I understand. If I understand what you say, D for all methods (and functions) can raise exceptions, unless it has nothrow. And if I still includes exceptions that are thrown are at the time of compilation. So I can not write: public void testMe () throws MyException {} However, if I write this and my method throws an exception, it will take place at compile time: public void testMe () {} And if do not want an exception thrown, I should write: public void testMe () : nothrow {} or perhaps : public void testMe () pure nothrow safe {} Is that correct? PS: Thanks for the article on the interveiw Anders Hejlsberg, it enlightens me a little more about how exceptions sen D (andHello everyone, in java, you can have exceptions on methods. Thus we can write: public static void control (String string) throws MyException {} Is that possible in D and if so how does it work? If I write this D: public void testMe () throws MyException {} The compiler refuses to compile. What is the proper behavior for this D? thank youAt this point, the programming community at large seems to have decided that while checked exceptions seem like a good idea, they're ultimately a bad one. This article has a good explanation from one of the creators http://www.artima.com/intv/handcuffs.html At this point, Java is the only language I'm aware of which has checked exceptions (though there may be a few others somewhere), and newer languages have learned from Java's mistake and chosen not to have them. What D has instead is the attribute nothrow. Any function marked with nothrow cannot throw an exception. e.g. auto func(int bar) nothrow {...} It's similar to C++11's noexcept except that it's checked at compile time (like Java's checked exceptions), whereas noexcept introduces a runtime check. If a function is not marked with nothrow, then the only ways to know what it can throw are to read the documentation (which may or may not say) or to read the code. There are obviously downsides to that in comparison to checked exceptions, but the consensus at this point is that it's ultimately better. - Jonathan M Davis
May 08 2014
On Thursday, 8 May 2014 at 13:06:05 UTC, amehat wrote:Okay. Thank you for these explanations, I understand a little better the exceptions D.Keep in mind that D also has the concept of "Error". Both "Exception" and "Error" derive from "Throwable". "nothrow" only means the function will not throw an *Exception*. "Error" can be thrown any time, from anywhere. They bypass the nothrow, bypass destructor cleanup, and fly past "catch (Exception)". An Error is basically: "A critical Error has occurred. Salvage what you can before dying."
May 08 2014
On Thursday, 8 May 2014 at 14:02:06 UTC, monarch_dodra wrote:On Thursday, 8 May 2014 at 13:06:05 UTC, amehat wrote:I do not know the mistakes, at least not as you speak. I just read on http://dlang.org/errors.html errors. You mean errors and exceptions are almost identical?Okay. Thank you for these explanations, I understand a little better the exceptions D.Keep in mind that D also has the concept of "Error". Both "Exception" and "Error" derive from "Throwable". "nothrow" only means the function will not throw an *Exception*. "Error" can be thrown any time, from anywhere. They bypass the nothrow, bypass destructor cleanup, and fly past "catch (Exception)". An Error is basically: "A critical Error has occurred. Salvage what you can before dying."
May 08 2014
On Thu, May 08, 2014 at 03:19:04PM +0000, amehat via Digitalmars-d-learn wrote:On Thursday, 8 May 2014 at 14:02:06 UTC, monarch_dodra wrote:[...]What he meant, is that in D there are two kinds of throwables: Throwable / \ Exception Errors The 'nothrow' attribute means that the function will not throw an Exception. But it does not guarantee that it will not throw something else, like an AssertError (which is a subclass of Throwable but not a subclass of Exception). The intention is that "normal" exceptions must all inherit from class Exception. Exceptions represent recoverable problems that can be handled by catch blocks. Other Throwable subclasses, like Errors, represent non-recoverable problems with the program. E.g., AssertError means a wrong logic in the program was detected by an assert statement, so the program is in an inconsistent state that cannot be recovered from. So you should never catch an Error. Or if ever you do, you should make sure you terminate the program immediately afterwards. Also, note that some language guarantees will not hold when inside an Error catch block, so it's a bad idea to do anything other than emergency cleanup and then terminate the program. T -- Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert HubbardKeep in mind that D also has the concept of "Error". Both "Exception" and "Error" derive from "Throwable". "nothrow" only means the function will not throw an *Exception*. "Error" can be thrown any time, from anywhere. They bypass the nothrow, bypass destructor cleanup, and fly past "catch (Exception)". An Error is basically: "A critical Error has occurred. Salvage what you can before dying."I do not know the mistakes, at least not as you speak. I just read on http://dlang.org/errors.html errors. You mean errors and exceptions are almost identical?
May 08 2014