Thread overview
what is try-catch-finally?
Apr 01, 2004
kinghajj
Apr 01, 2004
J C Calvarese
Apr 01, 2004
Derek Parnell
Apr 01, 2004
Kris
Apr 02, 2004
Walter
Apr 02, 2004
Kris
Apr 02, 2004
Walter
April 01, 2004
Can somebody please post some good that shows how it is used? I don't understand it.


April 01, 2004
kinghajj wrote:
> Can somebody please post some good that shows how it is used? I don't understand
> it.  

Define good.

Look at:
http://www.dsource.org/tutorials/index.php?show_example=33

-- 
Justin
http://jcc_7.tripod.com/d/
April 01, 2004
On Thu, 1 Apr 2004 03:39:44 +0000 (UTC) (01/Apr/04 01:39:44 PM)
, kinghajj <kinghajj_member@pathlink.com> wrote:

> Can somebody please post some good that shows how it is used? I don't understand
> it.
>
>

I know what you mean. It was not until I saw some examples that the D documentation started to make any sense. The general format is ...

  try { <some statement(s) }
  catch (<errorclass>) { <do something about it> }
  finally { <always run> };

Basically, it is saying ...

Have a go at running the statements in <some statements> but if any errors are detected, jump immediately to the catch phrase. This is where is gets a bit complex. There are three varieties of catch phrase...

  catch { ... }  // This catches all errors
  catch ( classname ) { ... } // This only catches errors that are of the type 'classname'.
  catch ( classname X ) { ... } // This only catches errors that are of the type 'classname' and pass you an instance of that class in 'X'.

You can have any number of non catch-all phrases in a "try" statement but if you have catch-all phrase, it must be the last one in the try statement.

The 'finally' phrase contains some staements that execute whether an error was detected or not. It always executes.

You can define your own error classes like this example ...

  class myError
  {
      private:
         int vCode;
         char[] vMsg;

      public:
         // These internal variables get set when the error is thrown.
         this(int c, char [] m)
            {vCode = c; vMsg = m;}

      // Used to extract data by the catcher about the thrown error.
      int Code() {return vCode;}
      char[] Msg() { return vMsg;}
  }


Then in your application code, you can invoke this error by ...

  void FuncA()
  {
    . . .
    if (result == 0)
      throw new myError(23, "A very bad thing just happened!");
    . . .
  }


This means that you can catch this by doing ...


   try{
        FuncA( );
      }
   catch (myError E) { printf("ABEND: Error #%d, '%.*s'\n", E.Code, E.Msg); exit(1);}
   catch {printf("Something else went bad."); }

-- 
Derek
April 01, 2004
Karl,

I recommend you go to this address: http://www.mindview.net/Books/TIJ/

Download Mr Eckel's most excellent, and free, "Thinking in Java" book. Then, skip to chapter 9 and read all about exceptions. Both D and Java follow the same model, so you're in good hands with Mr Eckel.

- Kris



"kinghajj" <kinghajj_member@pathlink.com> wrote in message news:c4g2u0$14d8$1@digitaldaemon.com...
> Can somebody please post some good that shows how it is used? I don't
understand
> it.
>
>


April 02, 2004
"Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c4i35h$1arg$1@digitaldaemon.com...
> Download Mr Eckel's most excellent, and free, "Thinking in Java" book.
Then,
> skip to chapter 9 and read all about exceptions. Both D and Java follow
the
> same model, so you're in good hands with Mr Eckel.

Not exactly. Java uses the try-catch-finally model exclusively for exception error handling. C++ uses the try-catch-riaa model exclusively for exception error handling. The Java approach can get klunky looking when going beyond the easy cases, and can be tedious and error prone to get right. The C++ approach is more bulletproof, but without the finally sometimes one finds oneself creating dummy classes just to emulate a 'finally' clause.

D accommodates both styles by having both a finally and an riaa style cleanup.


April 02, 2004
Whoops! Thanks for the clarification.

- Kris


"Walter" <walter@digitalmars.com> wrote in message news:c4jafo$96k$2@digitaldaemon.com...
>
> "Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c4i35h$1arg$1@digitaldaemon.com...
> > Download Mr Eckel's most excellent, and free, "Thinking in Java" book.
> Then,
> > skip to chapter 9 and read all about exceptions. Both D and Java follow
> the
> > same model, so you're in good hands with Mr Eckel.
>
> Not exactly. Java uses the try-catch-finally model exclusively for
exception
> error handling. C++ uses the try-catch-riaa model exclusively for
exception
> error handling. The Java approach can get klunky looking when going beyond the easy cases, and can be tedious and error prone to get right. The C++ approach is more bulletproof, but without the finally sometimes one finds oneself creating dummy classes just to emulate a 'finally' clause.
>
> D accommodates both styles by having both a finally and an riaa style cleanup.
>
>


April 02, 2004
You mean raii (Resource Acquisition Is Initialization).

In article <c4jafo$96k$2@digitaldaemon.com>, Walter says...
>
>
>"Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c4i35h$1arg$1@digitaldaemon.com...
>> Download Mr Eckel's most excellent, and free, "Thinking in Java" book.
>Then,
>> skip to chapter 9 and read all about exceptions. Both D and Java follow
>the
>> same model, so you're in good hands with Mr Eckel.
>
>Not exactly. Java uses the try-catch-finally model exclusively for exception error handling. C++ uses the try-catch-riaa model exclusively for exception error handling. The Java approach can get klunky looking when going beyond the easy cases, and can be tedious and error prone to get right. The C++ approach is more bulletproof, but without the finally sometimes one finds oneself creating dummy classes just to emulate a 'finally' clause.
>
>D accommodates both styles by having both a finally and an riaa style cleanup.
>
>

info@rayfract.com
April 02, 2004
"Siegfried Rohdewald" <Siegfried_member@pathlink.com> wrote in message news:c4jmkk$sbf$1@digitaldaemon.com...
> You mean raii (Resource Acquisition Is Initialization).

Yes <g>. I always goof that spelling up.