locked
is it possible to catch 2 exceptions at one time?(first judge if it is sql exception,if not, throw as normal exception)thank u very much RRS feed

  • Question

  • hi

       i would like to handle exception thatif it is sql exception,then handel and get some data from this sql exception,otherwise get information as normal exception.

       i tried this code ,and it was wrong.also i cannot write code of one try with 2 catches.may i ask how to deel with it?thank u very much in advance.

    best regards

    martin

    (my code)

       try
                {
                    this.Collection1.Save();
                    MessageBox.Show("Item saved!"));

                }

                catch (Exception ex) {
                    if (ex is System.Data.SqlClient.SqlException) {
                        System.Data.SqlClient.SqlException a =  System.Data.SqlClient.SqlException(ex);
       MessageBox.Show(a.eroecode.tostring()));
                    
                    }

    else{

    MessageBox.Show(ex.message);

    }               
                
                
                }

    Thursday, July 25, 2013 8:19 AM

Answers

  • This will work fine. Basically you don't need if statement inside catch block.. because catch block is itself selecting the right exception.

    You have to make sure that specific exception handers come earlier and generic one, come later.

    Also, just creating a new new exception is not enough. You must throw the newly construction exception object as shown below.

    try {
    
    ..... // code
    
    }
    
    catch(SQLException sqlEx) {
    
    MyException newEx = new MyException();
    
    throw newEx;
    
    }
    
    catch(Exception ex) {
    
    MessageBox.Show("bla");
    
    }



    val it: unit=()

    • Proposed as answer by d347hm4n Thursday, July 25, 2013 9:13 AM
    • Marked as answer by Caillen Wednesday, July 31, 2013 11:42 AM
    Thursday, July 25, 2013 8:30 AM
  • yes you can do it, I mean to say you can have multiple catch for one try, is it upto for catching exception in order, you can keep sql exception first and then generic exception later.

     try
                {
    
                    this.Collection1.Save();
                    MessageBox.Show("Item saved!"));
                }
                catch (SqlException sqlException)
                {
                    var message = sqlException.Message;
                }
                catch (Exception exception)
                {
                    var message = exception.Message;
                }
    If your code come across with sql exception then sqlexception catch will handle if not generic exception will handle.


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:42 AM
    Thursday, July 25, 2013 8:37 AM

All replies

  • This will work fine. Basically you don't need if statement inside catch block.. because catch block is itself selecting the right exception.

    You have to make sure that specific exception handers come earlier and generic one, come later.

    Also, just creating a new new exception is not enough. You must throw the newly construction exception object as shown below.

    try {
    
    ..... // code
    
    }
    
    catch(SQLException sqlEx) {
    
    MyException newEx = new MyException();
    
    throw newEx;
    
    }
    
    catch(Exception ex) {
    
    MessageBox.Show("bla");
    
    }



    val it: unit=()

    • Proposed as answer by d347hm4n Thursday, July 25, 2013 9:13 AM
    • Marked as answer by Caillen Wednesday, July 31, 2013 11:42 AM
    Thursday, July 25, 2013 8:30 AM
  • yes you can do it, I mean to say you can have multiple catch for one try, is it upto for catching exception in order, you can keep sql exception first and then generic exception later.

     try
                {
    
                    this.Collection1.Save();
                    MessageBox.Show("Item saved!"));
                }
                catch (SqlException sqlException)
                {
                    var message = sqlException.Message;
                }
                catch (Exception exception)
                {
                    var message = exception.Message;
                }
    If your code come across with sql exception then sqlexception catch will handle if not generic exception will handle.


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Caillen Wednesday, July 31, 2013 11:42 AM
    Thursday, July 25, 2013 8:37 AM
  • thank u very much for ur kindness help
    Wednesday, July 31, 2013 12:07 PM