I am creating a multi-layered application using dlls' and want to throw any exception that occurs (and also create my own exceptions to throw) back to the originating code. When I try this though I get an error that the exception is unhandled. I am trying to handle it by just throwing it. This occurs at the first throw which is in class a catch clause. The following dll deals with database access.
The code snippet is :
class a{
public object ExecuteScalar(QueryBuilder QB){ object res = ""; try{ string sql = BuildSQL(QB); // connect to the database and run the query SqlCommand cmd = new SqlCommand(sql, DBConnections.SQLConn); DBConnections.SQLConn.Open();res = cmd.ExecuteScalar();
DBConnections.SQLConn.Close();}
catch{ // system error if (DBConnections.SQLConn.State != System.Data.ConnectionState.Closed) DBConnections.SQLConn.Close(); throw; } return res;}class b{
public object GetObject(string UDOName){try{
a mine
= new a();res = mine.ExecuteScalar(QB).ToString();
} catch {
throw;
}}
class c{
private void btnGetObj_Click(object sender, EventArgs e){
try{
b
udo = new b; string res = udo.GetObject("testing").ToString();txtGetObj.Text = res;
} catch {
// handle the error here
}
Any ideas on why this happens I am using VS2005, .Net 2.0.
Thanks in advance
karen

Throw Exception gets unhandled error
abhishek_6023
So user can know What exception was throun and why it was thrown (Which wil be know by Custom Exception's Properties)
Best Regards,
Jeff Williams
Thanks!
I was trying to do that but needed to make sure to close the connection to the database if there was an exception. I had put a Try/catch to close the DB but that was not allowing the exception to go back. Finally figured out I could use a try/catch/finally, using the finally to close the database and removing the catch.
Karen
Reino
I was thinking that the catch block was required with any try... also thought that you could use the 'throw' keyword to send a system exception back. If I understand you only use the throw command to throw a new user generated exception
Such as --- throw new ArgumentException(“Exception”);
Is it possible in .Net 1.1 to use the throw keyword to throw a system exception
I have seen several examples such as
try {//something} catch(DivideByZeroException e) { throw; }
But that does not work int .Net 2.0.
Karen
Matthew_Hlavacek
Is there a way to send back the pure system exception
Roger Lipscombe
Your question was something different so I did not know what were you actually wanting.
finally is the block which always executes if exception occurs or not so u can close connection in finally and you dont need to use catch at all, Just exception be thrown and caught in calling code.
Best Regards,
PedroCGD
2 Solutions for this:
Now think what is better for you.
Best Regards,