How to handle try-catch blocks, nothrow and logfiles
Tim via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 24 10:09:23 PDT 2014
I'm working on an application where I want log all exceptions but
I'm not sure what's the best way to realize that. Sure I can do
the following:
void myMethod() nothrow
{
try
{
// Do something
}
catch (Exception e)
{
// Write log file
}
}
But there are some problems:
1) try-catch are working great for local error handling. But
they are nearly useless for global error handling. Imagine I've
an application where I want log all thrown exceptions. Sure, I
can create a Logfile-class and log all exceptions in the
catch-block. But doing this in all my methods forces me to create
dirty code (more braces, more idents because of more braces, more
lines and so on...). Additionally when I change the interface of
my Logfile-class I need to adapt all catch-blocks. I think that's
not an optimal solution. So, is there anything I can use to log
all exceptions in one handler, for instance something like that:
void handleExceptions(Exception e)
{
// Write to log file
}
void myMethod() throwTo(handleExceptions)
{
// Call something that probably throws an exception
}
2) When I want create an application where all methods are
defined as >nothrow< - how can I realize that? Writing to a
log-file is already throwable. Yes, I can catch the exception and
write the same to the command line but that's also unsafe... are
there any nothrow-function to write a file or something to the
command line? It's a bit sad when I want log an exception and I'm
unable to write the log entry because something happened. So how
can make sure that I log all exceptions? Is that even possible?
More information about the Digitalmars-d-learn
mailing list