www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Particular exceptions names

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
In next example code, it used user-made exception, but what if 
I'm looking for a particular exception? from where can I get 
particular exception to arise it?


     import std.stdio;
     import std.string;

     string division(int a, int b) {
        string result = "";

        try {
           if( b == 0 ) {
              throw new Exception("Cannot divide by zero!");
           } else {
              result = format("%s",a/b);
           }
        } catch (Exception e) {
           result = e.msg;
        }

        return result;
     }

     void main () {
        int x = 50;
        int y = 0;

        writeln(division(x, y));

        y = 10;
        writeln(division(x, y));
     }

source site:
https://www.tutorialspoint.com/d_programming/d_programming_exception_handling.htm
Jul 26 2022
next sibling parent frame <frame86 live.com> writes:
On Tuesday, 26 July 2022 at 23:43:59 UTC, pascal111 wrote:
 In next example code, it used user-made exception, but what if 
 I'm looking for a particular exception? from where can I get 
 particular exception to arise it?
There is no mechanism to find a particular exceptions in D. You have simple to know which exception can be thrown by studying the documentation or source code. If you just want to give the user an information which exception was thrown in run time, use typeid: ```d try { ... } catch(Exception e) { writefln("%s was thrown", typeid(e)); } ```
Jul 27 2022
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/26/22 16:43, pascal111 wrote:
 In next example code, it used user-made exception,
I am not sure I understand you correctly because the program you show throws Exception, which is not user-made at all. If you want to throw a particual exception that you define, you need to inherit that type from Exception. The following program show an example as well as 'enforce', which I prefer over explicit if+throw+else: import std.stdio; import std.format; class MissingArguments : Exception { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } } void main(string[] args) { // if (args.length != 42) { // throw new MissingArguments(args.length); // } import std.exception : enforce; enforce!MissingArguments(args.length == 42, format!"Too few arguments: %s"(args.length)); // Program continues here... (No 'else' needed.) } Ali
Jul 27 2022
next sibling parent pascal111 <judas.the.messiah.111 gmail.com> writes:
On Wednesday, 27 July 2022 at 09:35:12 UTC, Ali Çehreli wrote:
 On 7/26/22 16:43, pascal111 wrote:
 [...]
I am not sure I understand you correctly because the program you show throws Exception, which is not user-made at all. [...]
It seems an advanced topic. It'll take some more studying to understand this code.
Jul 27 2022
prev sibling parent kdevel <kdevel vogtner.de> writes:
On Wednesday, 27 July 2022 at 09:35:12 UTC, Ali Çehreli wrote:

 The following program show an example as well as 'enforce', 
 which I prefer over explicit if+throw+else:
Me too.
   enforce!MissingArguments(args.length == 42,
                            format!"Too few arguments: 
 %s"(args.length));
However, this particular form lowers the readability a lot. It is not the absence of arguments which is enforced but the length of the args array, i.e. the presence of the arguments. Therefore I prefer enforce (args.length == 42, new MissingArguments (...));
Jul 27 2022
prev sibling parent kdevel <kdevel vogtner.de> writes:
On Tuesday, 26 July 2022 at 23:43:59 UTC, pascal111 wrote:
 In next example code, it used user-made exception,
 [...]
        try {
           if( b == 0 ) {
              throw new Exception("Cannot divide by zero!");
           } else {
              result = format("%s",a/b);
           }
 [...]
     void main () {
        int x = 50;
        int y = 0;
What about the case x = -2147483648, y = -1?
Jul 27 2022