What is the best way to stop App after exception?

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 15 07:17:01 PST 2016


On Monday, 15 February 2016 at 11:38:05 UTC, Suliman wrote:
> I have got class Config with method parseconfig. I need 
> terminate App if parsing of config was failed. The problem that 
> I do not know how to do it better.
>
> void parseconfig()
> {
>  try
>  {
>   //something go wrong
>  }
>
> catch(Exception e)
>  {
>   writeln(e.msg);
>   // throw any exception here
>  }
> }
>
>
> But my main.d also have try catch block and parseconfig() calls 
> inside it. The problem that wen exception rise I do not know 
> how to terminate app. Exception simply print on screen and app 
> is continue.
>
> void main()
>  {
>  try
>   {
>    parseconfig(); // exception was already handled inside:  
> void parseconfig()
>   }
>  }
>
> What is the best practice to stop app?

Since C's "exit" function is not liked, best thing you can do is 
to throw an Error when you want to close the program. You are not 
supposed to catch Errors. So, it eventually will stop the 
currently running thread.

BUT (This is a big but with single t), in multithreaded process, 
throwing Error in a thread that is not the main thread won't stop 
your process still and you are still left with "exit" function.


More information about the Digitalmars-d-learn mailing list