I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it.
Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 15, 2021 Exceptions names list | ||||
---|---|---|---|---|
| ||||
November 15, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote: >I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it. Just so I understand, do you mean a list similar to this? |
November 15, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On 11/15/21 2:28 PM, pascal111 wrote: > I want to know where the list of exceptions names that "throw" statement > uses. Such a list is not very useful. Normally all we need is Exception and occasionally one or two other specific types. There can be hundreds of specific types of Exception a program may throw, sometimes unknowingly by calling a library that it uses. That list can change dramatically simply by adding a single line to the program. (That line may call a new module and that module may throw dozens of different Exception types.) What matters is they are all Exceptions. So, most of the time all one needs to do is to catch Exception in main and be done with it: int main() { try { work(); } catch (Exception ex) { stderr.writefln!"ERROR: %s"(ex.msg); return 1; } return 0; } All of my programs use that idiom. (Also note: Error is not being caught; just Exception.) That try-catch block covers all Exception types that may be thrown. The user gets a useful message and we are done. Except, in some cases a specific exception type is useful. For example, I needed to catch ConvException that I know std.conv may throw. (This is documented on std.conv documentation.) In that case, I catch that exception in a lower-level function to give the user an understandable error message like "Failed to convert 'hello' to an integer" so that the user can try entering new data instead of exiting the whole program. Ali |
November 15, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote: >I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it. Just so I understand, do you mean a list similar to this? Exactly! |
November 15, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote:
> On 11/15/21 2:28 PM, pascal111 wrote:
> > [...]
> "throw" statement
> > [...]
>
> Such a list is not very useful. Normally all we need is Exception and occasionally one or two other specific types.
>
> [...]
What if I expect more than one exception a function can do in "try" block, and I need more than one "catch" block after that, so I'll need to define the exception, or you can turn around this with some way?
|
November 16, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote: >On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote: >I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it. Just so I understand, do you mean a list similar to this? Exactly! You can begin here: |
November 16, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Tuesday, 16 November 2021 at 00:03:14 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote: >On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote: >I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it. Just so I understand, do you mean a list similar to this? Exactly! You can begin here: If there is no such similar list we should create one. |
November 16, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Tuesday, 16 November 2021 at 00:05:45 UTC, Imperatorn wrote: >On Tuesday, 16 November 2021 at 00:03:14 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:55:55 UTC, pascal111 wrote: >On Monday, 15 November 2021 at 22:39:36 UTC, Imperatorn wrote: >On Monday, 15 November 2021 at 22:28:28 UTC, pascal111 wrote: >I want to know where the list of exceptions names that "throw" statement uses. I searched in dlang site, and google but couldn't reach it. Just so I understand, do you mean a list similar to this? Exactly! You can begin here: If there is no such similar list we should create one. I just did a quick grep of phobos and matched the derived exceptions and got this: |
November 15, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On 11/15/21 3:00 PM, pascal111 wrote: > On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote: >> On 11/15/21 2:28 PM, pascal111 wrote: >> > [...] >> "throw" statement >> > [...] >> >> Such a list is not very useful. Normally all we need is Exception and >> occasionally one or two other specific types. >> >> [...] > > What if I expect more than one exception a function can do in "try" > block, One benefit of exceptions is separating actual work from error handling. Code can be written as if everything will go well. If not, a higher level function may catch the exception and e.g. start over. So, no matter how many different kinds of exceptions may be thrown or no matter how complicated a function is, we just do the work: void foo() { work(); moreWork(); etc(); } Some higher level function may have a try-catch, which may catch a potential exception. Fine: // An intermediate function that uses foo: void zar() { foo(); } // A higher level function that uses the intermediate function: void bar() { // Try until successful while (true) { try { zar(); // It worked! Let's leave. return; } catch (Exception ex) { // Something bad happened // Let's try again... } } } bar() catches all exceptions without caring what exact type they are. (In practice, for me, bar() is usually main(), the top level function.) > and I need more than one "catch" block after that, so I'll need > to define the exception, or you can turn around this with some way? Well, if you *need* to handle a particular exception specially, then you have to have a specific catch clause for that exception. Otherwise, a single catch(Exception) is sufficient for most programs. That is possible because all exceptions inherit from Exception: class MyException : Exception { // ... } That definition means "MyException is an Exception", which allows MyException objects to be caught as Exceptions as well. Exceptions are a useful tool but they are not suitable for all cases. Maybe you have such a case. Ali |
November 16, 2021 Re: Exceptions names list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote:
> On 11/15/21 2:28 PM, pascal111 wrote:
> > I want to know where the list of exceptions names that
> "throw" statement
> > uses.
>
> Such a list is not very useful. Normally all we need is Exception and occasionally one or two other specific types.
>
It's actually very useful when you want to throw exceptions yourself, because instead of writing your own custom exception then you could use an existing implementation.
|