Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
See more:
i m using asp.net,C#,VS 2005,sql server 2005..

is it posssible to catch all the exception in C# asp.net?

i m using

try
{
}
catch(Exception ex)
{
}


but this try block does not catch InvalidOperationException etc...

is there any way to catch all exception using one catch block?

plz help me

regards
karan

From a comment from the OP:
try 
{
  connection.Open();
  st = connection.BeginTransaction();
  commandToFinding = new SqlCommand(sqlqueryToFinding, connection,st);
  if (numToFinding != 0)
  {
    st.Commit();
    Response.Redirect("AddStudy.aspx?stid=" + StudyIDTextBox.Text);
    connection.Close();
  }
}
catch(Exception)
{
  st.Rollback(); // Exception happens here (This SqlTransaction has completed; it is no longer usable.)
  connection.Close();
}
Posted
Updated 13-May-11 8:49am
v4
Comments
Sergey Alexandrovich Kryukov 13-May-11 14:09pm    
Not true! It's no good to post something as facts without checking them up. My 1.
--SA

I do not agree with you. InvalidOperationException is inherited from Exception class. Any excpetion thrown would be handled by the code block you have shown.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-May-11 14:15pm    
You're absolutely right.
As to catching all, propagation, re-throwing and design of it --
please see my answer.
--SA
Perhaps you're catching an exception and then throwing another exception in your catch block? After all, according to your comments to other answers, you're performing DB operations in your catch block (a rollback and closing a connection). I haven't looked to see if either of those operations could throw your particular exception, but you should check.
 
Share this answer
 
Comments
karan joshua 13-May-11 9:47am    
it is giving like :This SqlTransaction has completed; it is no longer usable.it is showing near :

catch(Exception)
{
st.Rollback();<-------------here
connection.Close();
}

why it is so? when there is no exception in inserting the record to the DB why it is showing that error...
Marc A. Brown 13-May-11 9:54am    
Exactly. You're handling one exception and then generating a new one in your exception handler. I can't tell you why you're getting the initial exception, nor why you're generating a new one in the catch block, but that's what's happening.
ZeeroC00l 13-May-11 9:57am    
Nicely spotted :)
karan joshua 13-May-11 10:06am    
why it is going to catch block when there is no exception?

k thank u...

tell me this...
catch
{
if(st!=null)
st.Rollback();
}

Is it st!=null or st==null?
Marc A. Brown 13-May-11 10:14am    
Seriously? You can't *get* to the catch block unless your code has thrown an exception that's handled by that catch. The code in your try block *is* throwing an exception. In your catch block, you're going to need to either check to make sure a rollback is appropriate before doing it or wrap it in a try to handle the exception that it's throwing. I'd also recommend doing something to see what exception you're handling in the first place that's causing the exception in the catch block. Again, you wouldn't be in the catch block if you hadn't thrown an exception to get there in the first place.
Did you consider Making use of Multiple catch blocks ??

Why dont you make you catch block as follows and try ?
C++
catch ( InvalidOperationException e )
{
 // Perform task
}
catch ( Exception e )
{
 // Perform Task
}
finally
{
// Perform Talk
}

BR//
Harsha
 
Share this answer
 
v2
Comments
karan joshua 13-May-11 10:02am    
It works... but i want to catch all exception...there r many exception ...Can we write catch block for every exception?
ZeeroC00l 13-May-11 10:06am    
That's why i have mentioned Multiple Catch blocks.
What i suggest is Make sure you have Catch blocks for some standard exception that you expect from the code and for the rest use a general catch block.

catch( InvalidOperationException e)
{
}
//few more specific exceptions
catch
{
// handle general Exception
}
karan joshua 13-May-11 10:14am    
k .. Is there any way to catch all standatd exception?
ZeeroC00l 13-May-11 10:19am    
You can either make use of a general catch block or handle the standard one seperately. Its up to you.

As Mentioned by @Marc in the above answer, however you handle the catch block, make sure that that content inside the catch block wont throw another exception.
karan joshua 13-May-11 11:10am    
yes zeeroCool u r correct... .... actually it is working fine now...

one simple coding mistake that is i have written st.Rollback(); directly instead of :if(st==null)
st.Rollback();

but i got code like st!=null then st.rollback(); from net .. actuly it is wrong ...it should be
if(st==null)
st.Rollback();

thank u very much for your patience...

thank u...thank u...thank u...thank u...thank u...
It's very bad to catch all exception, but there are cases when it is needed.
All the exceptions should be caught only on the top of the stack of each thread, in main UI cycle and in some special cases. It's very important not to block exception propagation in all other cases.

For more detail, please see my past recommendations:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

—SA
 
Share this answer
 
v2
Comments
ZeeroC00l 13-May-11 14:17pm    
Great links :) My +5
Sergey Alexandrovich Kryukov 13-May-11 15:05pm    
Thank you very much,
--SA
Manas Bhardwaj 13-May-11 14:33pm    
nice... +5
Sergey Alexandrovich Kryukov 13-May-11 15:05pm    
Thank you, Manas.
--SA
Marc A. Brown 13-May-11 14:42pm    
Good call. My 5.
InvalidOperationException is inherited from the System.Exception class.

So the catch block should be able to catch this type of exception also.

Try{} Catch(){} block is used to catch the exception on RunTime not on the Compile Time.

So check again. and correct me if I am wrong
 
Share this answer
 
Comments
karan joshua 13-May-11 9:24am    
try
{
connection.Open();
st = connection.BeginTransaction();
commandToFinding = new SqlCommand(sqlqueryToFinding, connection,st);
if (numToFinding != 0)
{
st.Commit();
Response.Redirect("AddStudy.aspx?stid=" + StudyIDTextBox.Text);
connection.Close();
}
}
catch(Exception)
{
st.Rollback();
connection.Close();
}

now it is giving error like InvalidperationException was unhandled by user code..

wht is the problem?
Simple answer is that your exception is not occurring within a try-catch.
 
Share this answer
 
Comments
karan joshua 13-May-11 9:28am    
try
{
connection.Open();
st = connection.BeginTransaction();
commandToFinding = new SqlCommand(sqlqueryToFinding, connection,st);
if (numToFinding != 0)
{
st.Commit();
Response.Redirect("AddStudy.aspx?stid=" + StudyIDTextBox.Text);
connection.Close();
}
}
catch(Exception)
{
st.Rollback();
connection.Close();
}

now it is giving error like InvalidperationException was unhandled by user code..

wht is the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900