www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope exception do not rise

reply "Suliman" <evermind live.ru> writes:
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
next sibling parent reply "Suliman" <evermind live.ru> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply "Suliman" <evermind live.ru> writes:
 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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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. However, judging by their names, they are both descendants of Exception, so what you are doing will catch either of them. Ali
Nov 06 2014
parent reply "Suliman" <evermind live.ru> writes:
 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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/06/2014 08:47 AM, 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.
Where I can look at hierarchy of exceptions?
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. Ali
Nov 06 2014
prev sibling parent reply "MadProgressor" <MadProgressor dub.jp> writes:
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
parent reply "Gary Willoughby" <dev nomad.so> writes:
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
Nov 05 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.
Nov 05 2014
parent "Suliman" <evermind live.ru> writes:
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