www.digitalmars.com         C & C++   DMDScript  

D - what is try-catch-finally?

reply kinghajj <kinghajj_member pathlink.com> writes:
Can somebody please post some good that shows how it is used? I don't understand
it.
Mar 31 2004
next sibling parent J C Calvarese <jcc7 cox.net> writes:
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/
Mar 31 2004
prev sibling next sibling parent "Derek Parnell" <Derek.Parnell psyc.ward> writes:
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
Mar 31 2004
prev sibling parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
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

 it.

Apr 01 2004
parent reply "Walter" <walter digitalmars.com> writes:
"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.

 skip to chapter 9 and read all about exceptions. Both D and Java follow

 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.
Apr 02 2004
next sibling parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
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.

 skip to chapter 9 and read all about exceptions. Both D and Java follow

 same model, so you're in good hands with Mr Eckel.

Not exactly. Java uses the try-catch-finally model exclusively for

 error handling. C++ uses the try-catch-riaa model exclusively for

 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.

Apr 02 2004
prev sibling parent reply Siegfried Rohdewald <Siegfried_member pathlink.com> writes:
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.

 skip to chapter 9 and read all about exceptions. Both D and Java follow

 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
Apr 02 2004
parent "Walter" <walter digitalmars.com> writes:
"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.
Apr 02 2004