www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - returning to thrown

I have to modify preexisting code. As of now, the code fails and 
throws an exception at some point. I need to prevent the code 
from throwing(easy) but signal to the user of the code some 
notification(hard).

It would be cool if I could throw an exception as if the code 
yielded and it could be continued if I want.

simplified pseudoscope

void load()
{
   read(somefile);  <-- throws if somefile does not exist

   ...
}

goes in to

void load()
{
   try
   {
     file = read(somefile);  <-- throws if somefile does not exist
   }
   catch
   {
      file = null;
      throw new yeildException!4("File not found!");
   }

   yieldException!4:

   ...
}


then when one catches a yieldException somehow they can resume at 
the proper location to continue the function as if the exception 
never happened.

Because I don't have access to all the code nor do I want to 
rewrite large portions of it, it is impossible to redesign 
everything to make things work correctly.


I believe throw unwinds the stack which makes a yield semantic 
impossible?

I can't use a callback(too ugly and it would require modify huge 
amounts of code to add them everywhere unless it were global):


void load()
{
   try
   {
     file = read(somefile);  <-- throws if somefile does not exist
   }
   catch
   {
      file = null;
      userCallback("File not found!");
   }


   ...
}


Any devious D tricks that can be used to give the sort of 
yieldThrow I'm looking for?


BTW, if D had a yieldThrow which behaved like throw but saved the 
stack to rewind back to the point of yieldThrow, then one could 
simply do


void load()
{
   try
   {
     file = read(somefile);  <-- throws if somefile does not exist
   }
   catch
   {
      yieldThrow new yeildException!4("File not found!");
      // code returned back to this point when yieldThrow finished 
doing what it does.
      file = null;
   }


   ...
}


Remember, these errors occur inside some large processing(of a 
file) which might error depending on dependencies.  Some of these 
errors are not critical and I can't reparse the file each time 
because the same error will occur. I simply need to ignore the 
errors. I have access to the code where I can ignore them but I 
need some nice way to inform the user of the process that 
something didn't go as planed. Again, this wasn't defined in to 
the original code and it it already has it's own error handling.
Jun 12 2018