R
Roger Down
I have a question regarding exception handling and logging.
First let me explain the situation.
I am creating a .NET library with functionality that I am giving to another
team developer. This other developer does not want to be bother with all
sorts of exceptions from me, but only wants "controlled" return
codes/classes. This is because he wants to take action on whatever return
code / class I return. Makes it easier.
He want however me to log all exception to a common logging system.
An example code could look like this:
public ResultCode SomeMethod()
{
ResultCode resultCode = ResultCode.OK;
try
{
// Do something here...
}
catch( Exception e )
{
Log(e.Message);
resultCode = ResultCode.Error;
}
return resultCode;
}
The other developer can now just check the return code from the SomeMethod()
function.
The problem with the above code is that this is not a recommended way of
using the try & catch framework, because I use the general type Exception in
the catch block... and do not rethrow the exception.
Well... since I can't rethrow the exception I need to do something about the
catch block. I could rewrite the catch block to look something like this:
public ResultCode SomeMethod()
{
ResultCode resultCode = ResultCode.OK;
try
{
// Do something here...
}
catch( ExceptionType1 e1 )
{
Log(e1.Message);
resultCode = ResultCode.Error;
}
catch( ExceptionType2 e2 )
{
Log(e2.Message);
resultCode = ResultCode.Error;
}
catch( ExceptionType3 e3 )
{
Log(e3.Message);
resultCode = ResultCode.Error;
}
return resultCode;
}
This means I need to know every exception that can be thrown from methods in
the try block... The easiest way would be to use some type of general
Exception type, that will catch all those exceptions so I don't need to
write all those catch blocks.
But perhaps there are some easier (or other) ways of dealing with this
situation ?
Best of regards...
First let me explain the situation.
I am creating a .NET library with functionality that I am giving to another
team developer. This other developer does not want to be bother with all
sorts of exceptions from me, but only wants "controlled" return
codes/classes. This is because he wants to take action on whatever return
code / class I return. Makes it easier.
He want however me to log all exception to a common logging system.
An example code could look like this:
public ResultCode SomeMethod()
{
ResultCode resultCode = ResultCode.OK;
try
{
// Do something here...
}
catch( Exception e )
{
Log(e.Message);
resultCode = ResultCode.Error;
}
return resultCode;
}
The other developer can now just check the return code from the SomeMethod()
function.
The problem with the above code is that this is not a recommended way of
using the try & catch framework, because I use the general type Exception in
the catch block... and do not rethrow the exception.
Well... since I can't rethrow the exception I need to do something about the
catch block. I could rewrite the catch block to look something like this:
public ResultCode SomeMethod()
{
ResultCode resultCode = ResultCode.OK;
try
{
// Do something here...
}
catch( ExceptionType1 e1 )
{
Log(e1.Message);
resultCode = ResultCode.Error;
}
catch( ExceptionType2 e2 )
{
Log(e2.Message);
resultCode = ResultCode.Error;
}
catch( ExceptionType3 e3 )
{
Log(e3.Message);
resultCode = ResultCode.Error;
}
return resultCode;
}
This means I need to know every exception that can be thrown from methods in
the try block... The easiest way would be to use some type of general
Exception type, that will catch all those exceptions so I don't need to
write all those catch blocks.
But perhaps there are some easier (or other) ways of dealing with this
situation ?
Best of regards...