Re: What is the best way to stop App after exception?

2016-02-15 Thread Mike Parker via Digitalmars-d-learn

On Monday, 15 February 2016 at 15:38:07 UTC, Suliman wrote:

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 if I throw exception it's handled in instance of class 
Config and it's not go to main...




Either rethrow the original Exception:

catch(Exception e)
 {
  writeln(e.msg);
  throw e;
 }

In this case, if nothing else catches the exception, then it will 
go back to main. If anything else does catch it, it can rethrow.


Or you can do as suggested above throw a new Error:

catch(Exception e)
 {
  writeln(e.msg);
  throw new Error(e.msg);
 }

If nothing is catching Throwable or Error (and nothing should 
be), then the error will go all the way back to main just fine.




Re: What is the best way to stop App after exception?

2016-02-15 Thread Sean Campbell via Digitalmars-d-learn

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?


If it's an problem parsing config, it would be best practice to 
throw an exception or error, but for future reference, if you 
want to terminate execution use Runtime.terminate from 
core.runtime, followed by C's exit from core.stdc.stdlib. 
Runtime. terminate will do cleanup and allow the program to 
shutdown properly


Re: What is the best way to stop App after exception?

2016-02-15 Thread hjkl via Digitalmars-d-learn

On Monday, 15 February 2016 at 11:38:05 UTC, Suliman wrote:

What is the best practice to stop app ?


Iveseenthisnewlibtheotherday:https://github.com/John-Colvin/exitclean





Re: What is the best way to stop App after exception?

2016-02-15 Thread Ali Çehreli via Digitalmars-d-learn

On 02/15/2016 03:38 AM, 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.

As others said, exceptions inherited from Error indicate situations 
where the application cannot continue.


However, a function like parseconfig() does not sound like it would know 
whether the error that it detected is serious enough to terminate the 
whole application. It is conceivable that the caller can simply say 
"next file" and continue. So, I think parseconfig() should either 
propagate or throw an Exception and let the caller decide.


> void parseconfig()
> {
>   try
>   {
>//something go wrong
>   }
>
> catch(Exception e)
>   {

The way I see it, exceptions should be caught only if there is anything 
to be done with that error condition.


>writeln(e.msg);
>// throw any exception here

Needing to catch an exception just to log and then rethrow it is 
something that is against the whole spirit of exceptions mechanisms. It 
brings exceptions back to using error codes: Every function must do the 
same to leave behind a trail, presumably for debugging.


There are other options:

- Log before the error condition at a different logging level.

- Put a break point at the constructor of the exception. The back trace 
will show the backtrace.


- Automate the last two and call a library like libunwind and dump the 
backtrace from the constructor of exception. (However, you may not want 
to log every thrown exception.)


I know that sometimes the thrown exception does not carry enough 
information. Then it's ok to do what you're doing: catch and throw 
another exception.


>   }
> }
>
>
> 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.

I don't know how that happens. I am presuming you do throw another 
exception where you say


   // throw any exception here

Then all you need to do is catch in main, log the problem and return 
non-zero from main.


> void main()
>   {
>   try
>{
> parseconfig(); // exception was already handled inside:

But it did rethrow, right? Then it should be fine.

>  void
> parseconfig()
>}
>   }
>
> What is the best practice to stop app?

Ali



Re: What is the best way to stop App after exception?

2016-02-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 15, 2016 15:38:07 Suliman via Digitalmars-d-learn wrote:
> On Monday, 15 February 2016 at 15:17:01 UTC, tcak wrote:
> > 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 if I throw exception it's handled in instance of class Config
> and it's not go to main...
>
> And what the reason to not add support of C's "exit"?

You can call C's exit from D, but it won't shut down the app cleanly. It
_can't_ shut down the app cleanly. It can't even do that in C. For an
application to shut down cleanly, all of its threads must exit cleanly,
which requires that the programmer tell them to shut down cleanly (you can't
just have them magically exit cleanly when they're in the middle of
running), and of course, main has to exit properly so that the runtime has a
chance to shut things down properly. That can be by returning from main or
by having an Exception or Error bubble out of it, but simply calling exit()
would kill the program without letting the runtime - or anything in your
program - shut down cleanly.

- Jonathan M Davis



Re: What is the best way to stop App after exception?

2016-02-15 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 15, 2016 11:38:05 Suliman via Digitalmars-d-learn 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?

If you want your app to exit cleanly, it must exit via main. If an Exception
or Error is thrown from main (or bubbles up passed main), then the runtime
will catch it and print it out before exiting the program with a non-zero
exit value. If you don't want your program to exit via exception, then you
need to catch it in main, and if you've caught it in main, then you can just
exit main normally - either by reaching the end of its body or by returning.

But if you're handling the exception inside of another function and not
letting it bubble up to main or throwing another exception for main to
catch, then you're going to have to figure out how to have your functions
return until they get back to main - either that or have the exception reach
main.

- Jonathan M Davis



Re: What is the best way to stop App after exception?

2016-02-15 Thread Messenger via Digitalmars-d-learn

On Monday, 15 February 2016 at 15:17:01 UTC, tcak wrote:

[...]
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.


Mind, if you're doing the normal message passing thread dance, 
you can do periodic checks for an OwnerTerminated message.


http://melpon.org/wandbox/permlink/jxAzU0fcsnouMa9z


Re: What is the best way to stop App after exception?

2016-02-15 Thread Suliman via Digitalmars-d-learn

On Monday, 15 February 2016 at 15:17:01 UTC, tcak wrote:

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 if I throw exception it's handled in instance of class Config 
and it's not go to main...


And what the reason to not add support of C's "exit"?


Re: What is the best way to stop App after exception?

2016-02-15 Thread tcak via Digitalmars-d-learn

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.


What is the best way to stop App after exception?

2016-02-15 Thread Suliman via Digitalmars-d-learn
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?