Exception handling architecture

A

aftab Alam

Hi All,
I have am working on C# 2.0 for a project and need to know the best
practices for exception handling.
At the moment I am doing it as Follows.



I have a Manager class that is supposed to update entries in the Dictionary
and Database as well, to separate DataBase Layer I have written another
library that contains static methods and does some updating in data base..

The thing I want to know is that is it something that is the right way ? if
not please fuide me with resources...


its like


class PokerTableManager
{

Dictionary<string, PokerTable> PokerTablesDictionary;
public CreateTable(TableInfo info)
{
try
{
PokerTable table = new PokerTable(info);
PokerTablesDictionary.Add("abc",table );

}

}

}

classs PokerTableDAL
{
public static UpdateTable(TableInfo info)
{
try
{
/// SQL Commands here

}
catch(SqlException exp)
{
throw new DataBaseConnectoinException("UNable to connect
to database database",exp)
}

}

public class DataBaseConnectionException : Exception
{
public DataBaseConnectionException()
: base()
{ }
public DataBaseConnectionException(string message)
: base(message)
{ }
public DataBaseConnectionException(string message, Exception inner)
: base(message, inner)
{ }
}


regards

Aftab Alam
 
B

Brian Gideon

aftab said:
Hi All,
I have am working on C# 2.0 for a project and need to know the best
practices for exception handling.
At the moment I am doing it as Follows.



I have a Manager class that is supposed to update entries in the Dictionary
and Database as well, to separate DataBase Layer I have written another
library that contains static methods and does some updating in data base..

The thing I want to know is that is it something that is the right way ? if
not please fuide me with resources...


its like


class PokerTableManager
{

Dictionary<string, PokerTable> PokerTablesDictionary;
public CreateTable(TableInfo info)
{
try
{
PokerTable table = new PokerTable(info);
PokerTablesDictionary.Add("abc",table );

}

}

}

A try block must have at least one catch block or a finally block. The
code will not compile as is. Do you absolutely need the
try-catch-finally construct here anyway?
classs PokerTableDAL
{
public static UpdateTable(TableInfo info)
{
try
{
/// SQL Commands here

}
catch(SqlException exp)
{
throw new DataBaseConnectoinException("UNable to connect
to database database",exp)
}

}

When working with database connections I recommend either putting the
connection object in a using block or closing the connection in a
finally block. The code above could leave a connection open. Also, I
don't see a need for throwing a new exception after a SqlException has
been caught. Though, at least you've made the SqlException an inner
exception to the new exception.
public class DataBaseConnectionException : Exception
{
public DataBaseConnectionException()
: base()
{ }
public DataBaseConnectionException(string message)
: base(message)
{ }
public DataBaseConnectionException(string message, Exception inner)
: base(message, inner)
{ }
}

Again, I don't see a need for DataBaseConnectionException. Other than
a new class name it isn't providing you with anything new.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top