digitalmars.D.learn - scope exception do not rise
- Suliman (16/16) Nov 05 2014 void openFile(string fname, string current_folder)
- Suliman (24/24) Nov 05 2014 I can't understand what I am missing. Try-catch block also do not
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/30) Nov 05 2014 Unrelated to the problem, but did you really want that scope(success)
- Suliman (5/9) Nov 05 2014 that's work, but I can not understand where I can to look at
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/15) Nov 06 2014 We have to look at the documentation of the function. In this case the
- Suliman (1/6) Nov 06 2014 Where I can look at hierarchy of exceptions?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/17) Nov 06 2014 The topmost exception class is Throwable:
- MadProgressor (9/25) Nov 05 2014 Try:
- Gary Willoughby (3/6) Nov 05 2014 That shouldn't matter. See: http://dlang.org/exception-safe.html
- ketmar via Digitalmars-d-learn (4/5) Nov 05 2014 On Wed, 05 Nov 2014 14:09:20 +0000
- Suliman (8/8) Nov 05 2014 Am I right understand that keyword "Exception" is handle
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.
Nov 05 2014
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? } }
Nov 05 2014
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
Nov 05 2014
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)".
Nov 05 2014
On 11/05/2014 11:02 PM, Suliman wrote:We have to look at the documentation of the function. In this case the possibilities are FileException and UTFException. However, judging by their names, they are both descendants of Exception, so what you are doing will catch either of them. AliReplace 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)".
Nov 06 2014
We have to look at the documentation of the function. In this case the possibilities are FileException and UTFException. 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?
Nov 06 2014
On 11/06/2014 08:47 AM, Suliman wrote:The topmost exception class is Throwable: http://dlang.org/library/object/Throwable.html As mentioned there, there are only two descendants of it: Error and Exception. There is nothing else that can be said beyond that. Any function is free to throw any type as long as it is part of the hierarchy above. AliWe have to look at the documentation of the function. In this case the possibilities are FileException and UTFException. 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?
Nov 06 2014
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
Nov 05 2014
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 beforeThat shouldn't matter. See: http://dlang.org/exception-safe.html
Nov 05 2014
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.htmlthis indeed matter. and it should.
Nov 05 2014
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?
Nov 05 2014