How to handle exception without using try catch?

19,469

Solution 1

There is no other mechanism to handle an exception other than try catch. It sounds like you want something like

if(connection.DidAnErrorOccur)

but that doesn't exist.

Solution 2

ok, you can implementing de Application_Error in the global.asax, this method is the first line of defense for the errors and is for all the application

http://msdn.microsoft.com/en-us/library/24395wz3%28v=vs.100%29.aspx

for specific page for example default.aspx, you can implementing Page_Error

http://msdn.microsoft.com/en-us/library/ed577840%28v=vs.100%29.aspx

if you are working in mvc so you can implementing the OnException(ExceptionContext filterContext) in every controller that you want, this method is called when an unhandled exception occurs.

http://devproconnections.com/aspnet-mvc/aspnet-mvc-tutorial-handling-errors-and-exceptions

or you can implementing your error atribute

https://www.simple-talk.com/dotnet/asp.net/handling-errors-effectively-in-asp.net-mvc/

For another hand maybe you can use the Assert statement, with this you can evaluate a condition

http://msdn.microsoft.com/en-us/library/ttcc4x86.aspx

Solution 3

Only way is to check for all the conditions that would return an error. You should be doing this anyway. Try/catch is expensive. Try catch should be a last resort and there is no way around it for that purpose.

Solution 4

The best alternative I can give you is Elmah. http://code.google.com/p/elmah/ It will handle all your uncaught errors and log them. From there I would suggest fixing said errors or catching the specific errors you expect and not just catch any error that might occur. This is best to do so you can fix potential problems and not simply skip over code because an exception occurred.

Solution 5

Tejs' answer is correct, I believe there is no other mechanism to handle errors.

You can, however, handle more specific errors. You can also declare variables outside the try catch to see if it succeeded.

Example:

using (SqlConnection con = new SqlConnection())
{
     bool sqlErrorOccurred;

     try
     {
         con.Open();
         sqlErrorOccurred = false;
     }
     catch (SqlException ex)
     {
          sqlErrorOccurred = true;
     }

     if(sqlErrorOccurred)
     {
         MessageBox.Show("A Sql Exception Occurred");
     }
}
Share:
19,469
jams
Author by

jams

Updated on June 15, 2022

Comments

  • jams
    jams almost 2 years
    using (SqlConnection con = new SqlConnection())
    {
         try
         {
              con.Open();
         }
         catch (Exception ex)
         {
              MessageBox.Show(ex.Message);
         }
    }
    

    This works fine. But I want to know can we handle exception without using try catch like some thing if else? Or is it mendetory to use try catch.

  • GETah
    GETah about 12 years
    Unless you are developing a real time system (even this can be discussed) then no, Try/Catch are not expensive at all. Run a benchmark and you will see for yourself.
  • GETah
    GETah about 12 years
    How does this get rid of the try/catch?
  • KingOfHypocrites
    KingOfHypocrites about 12 years
    Using try/catch can affect compiler optimization and what programmer would use try/catch over doing something as simple as checking for null. It is just bad practice. Catching an exception will always be slower than doing a simple check. I'm not saying doing use them but don't use them in place of defensive programming.
  • KingOfHypocrites
    KingOfHypocrites about 12 years
    Your messagebox could go right in the catch statement. Just more code to do the same thing.
  • Scen
    Scen about 12 years
    I am simply illustrating concepts - this is not code you would use. As KingOfHypocrites said, you can put the messagebox in the catch statement. But there are far more complex things you couldn't just put in the catch statement.
  • Nick Turner
    Nick Turner over 10 years
    Try/Catch are expensive because you have to build the exception. Asking the customer to buy the newest Titanium server is just a bad idea. Most of the time it's better to not use them, while some can't be helped. It's a lot easier to test an Integer value than build another class chain and report it.