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

Reply via email to