A couple questions about a simple project

Andre Polykanine via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 17 10:08:54 PDT 2015


Hello John,

Yes, but this doesn't work, either.
Now I have this (that was my first variant, btw):

                                string s;
                                try {
                                        s = cast(string)std.file.read(f);
                                        try {
                                                check(s);
                                                if (this.verbose) {
                                                        output(format("%s: validation passed!", f));
                                                }
                                        } catch(CheckException e) {
// This is output when a file is invalid, that works
                                                output(format("Failed to validate %s: %s", f, e.toString()));
                                        }
                                } catch(Exception exc) {
//  And  that  isn't  output,  I  see  just  the  stacktrace as if the
exception was not caught at all
                                        output(format("Error reading %s: %s", f, exc.msg));
                                }
                        }

To  catch  the  exception,  I  need  to  wrap  the  validate() call in
`try...catch` in `main()`. Why so?
Thanks!
-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: @m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/


------------ Original message ------------
From: John Colvin via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
To: digitalmars-d-learn at puremagic.com
Date created: , 6:17:38 PM
Subject: A couple questions about a simple project


      On Monday, 17 August 2015 at 15:05:56 UTC, Andre Polykanine wrote:
> Hi everyone,
> I'm  new to D (I'm learning it by reading the great online book 
> by Ali
> Çehreli  -  thank  you  very  much for it, sir!), and, more 
> than that,
> programming  is  my hobby, so please bear with me if I'm asking 
> stupid
> questions.
> I've  made  a  toy  project  which  is  a small command-line 
> XML files
> validator:
> https://github.com/Oire/dxv/
> and I have a couple questions about it:
> 1.  I'm  using std.getopt but don't know how to make it display 
> a help
> message if no options are present at all:
> D:\repos\git\dxv\dxv.exe
> (nothing   happens   but   I   would  like it to show the help 
> as with
> --help switch)

Check if args has changed length after calling getopt, if not 
then it didn't find any options.

> 2. As you can see, I check whether the file to validate can be 
> read. I
> tried both `try...catch` and `enforce` (current version:
> `string  s  =  enforce(cast(string)std.file.read(f),  "Unable  
> to read
> file");`
> ),  but  this  very  exception  for  some reason can be caught 
> only in
> `main()`.
> What am I missing?
> Thanks!

string s;
try
{
     s = cast(string)std.file.read(f);
}
catch (FileException e)
{
     // do whatever you want.
}




More information about the Digitalmars-d-learn mailing list