Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2014 scope exception do not rise | ||||
---|---|---|---|---|
| ||||
void openFile(string fname, string current_folder) { auto file = readText(current_folder ~ fname); scope(failure) { writeln("failure"); } // writeln(file); } if file name do not exists, I want to rise scope exception. But it's do not rise, and I am getting only standard error like: std.file.FileException@std\file.d(191): D:\code\d\App1\source\app.d1: ╨Э╨╡ ╤Г╨┤╨ ░╨╡╤В╤Б╤П ╨╜╨░╨╣╤В╨╕ ╤Г╨║╨░╨╖╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗. ---------------- what's wrong? if I use block "success" it's work fine. |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | I can't understand what I am missing. Try-catch block also do not handle exception: void main() { string fname = "app.d1"; //file name with error string current_folder = (getcwd() ~"\\"); writeln(current_folder); openFile(fname, current_folder); } void openFile(string fname, string current_folder) { try { auto file = readText(current_folder ~ fname); if(exists(current_folder ~ fname)) scope(success) writeln("success"); } catch(Exception e) { writeln(e); //what class of error I am handling? any? } } |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 5 November 2014 at 12:56:41 UTC, Suliman wrote: > void openFile(string fname, string current_folder) > { > auto file = readText(current_folder ~ fname); > scope(failure) > { > writeln("failure"); > } > > // writeln(file); > > } > > if file name do not exists, I want to rise scope exception. But it's do not rise, and I am getting only standard error like: > > std.file.FileException@std\file.d(191): D:\code\d\App1\source\app.d1: ╨Э╨╡ ╤Г╨┤╨ > ░╨╡╤В╤Б╤П ╨╜╨░╨╣╤В╨╕ ╤Г╨║╨░╨╖╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗. > ---------------- > > what's wrong? if I use block "success" it's work fine. Try: -------- scope(failure){writeln("failure");} auto file = readText(current_folder ~ fname); -------- The scope(failure) is translated to a try catch after the satement you wann monitor. So put it before |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to MadProgressor | On Wednesday, 5 November 2014 at 14:04:26 UTC, MadProgressor wrote: > The scope(failure) is translated to a try catch after the satement you wann monitor. > So put it before That shouldn't matter. See: http://dlang.org/exception-safe.html |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby Attachments: | On Wed, 05 Nov 2014 14:09:20 +0000 Gary Willoughby via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > That shouldn't matter. See: http://dlang.org/exception-safe.html this indeed matter. and it should. |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | Am I right understand that keyword "Exception" is handle universal type of exceptions? catch (Exception) { writeln("inner"); } But in my example with try block can I change it's to something more informative? |
November 05, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 11/05/2014 06:01 AM, Suliman wrote: > I can't understand what I am missing. Try-catch block also do not handle > exception: I does. This turned out to be very tricky for me. :) > void main() > { > string fname = "app.d1"; //file name with error > string current_folder = (getcwd() ~"\\"); > writeln(current_folder); > openFile(fname, current_folder); > } > > void openFile(string fname, string current_folder) > { > try > { > auto file = readText(current_folder ~ fname); > if(exists(current_folder ~ fname)) > scope(success) > writeln("success"); Unrelated to the problem, but did you really want that scope(success) under the if statement? > } > > catch(Exception e) > { > writeln(e); //what class of error I am handling? any? > } Replace that with something like writeln("caught") and you will see that it is indeed caught. :) Printing the exception mimicks the default behavior and you (and I) think that the exception is not caught. :) Ali |
November 06, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > Replace that with something like writeln("caught") and you will see that it is indeed caught. :) Printing the exception mimicks the default behavior and you (and I) think that the exception is not caught. :)
that's work, but I can not understand where I can to look at exception level. If I right understand every function have own exceptions. For example std.file.
Where I could look at what "e" will get? I mean "catch(Exception e)".
|
November 06, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On 11/05/2014 11:02 PM, Suliman wrote: >> Replace that with something like writeln("caught") and you will see >> that it is indeed caught. :) Printing the exception mimicks the >> default behavior and you (and I) think that the exception is not >> caught. :) > > that's work, but I can not understand where I can to look at exception > level. If I right understand every function have own exceptions. For > example std.file. > Where I could look at what "e" will get? I mean "catch(Exception e)". > We have to look at the documentation of the function. In this case the possibilities are FileException and UTFException. http://dlang.org/phobos/std_file.html#.readText However, judging by their names, they are both descendants of Exception, so what you are doing will catch either of them. Ali |
November 06, 2014 Re: scope exception do not rise | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | > We have to look at the documentation of the function. In this case the possibilities are FileException and UTFException.
>
> http://dlang.org/phobos/std_file.html#.readText
>
> However, judging by their names, they are both descendants of Exception, so what you are doing will catch either of them.
>
Where I can look at hierarchy of exceptions?
|
Copyright © 1999-2021 by the D Language Foundation