locked
better way to swallow excpetion RRS feed

  • Question

  • I want to ignore a specific exception which is coming from thrid party control.

    How should i ignore the exception and continue the normal working?


    maifs

    Tuesday, December 13, 2011 11:33 AM

Answers

  • Hello,

    The best way to swallow an exception is to either handle it or rethrow it!

    You shouldn't swallow exceptions, there's a good reason you get this exception, so the question should be about how you handle it rather than how you swallow it.


    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 11:40 AM
  • Although it's not a bad practice to ignore exceptions, I think you want the following:

    try
    {
    //...your code where you use the third party control
    }
    catch(Exception)
    {
    //...swallow exception
    }


     

    May I recommend to still add some logging (so in the catch-block, write to your log that the exception was thrown) and provide any feedback to the third party vendor so they can fix this issue.

     


    Dimitri C. - Please mark the replies as answers if they help! Thanks.
    • Edited by Dimitri C Tuesday, December 13, 2011 11:40 AM
    • Proposed as answer by JMCF125 Tuesday, December 13, 2011 3:55 PM
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:25 AM
    Tuesday, December 13, 2011 11:39 AM
  • What do you mean by still raised? The exception is not caught and the application is crashing?

    If yes, I guess AddThirdPartyObject() function is dealing with some unmanaged code which is failing (possibly memory corruption issue).


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 1:22 PM
  • hopefully it's not a corrupted state exception that you're trying to swallow.

    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:08 PM
  • You could find out why the exception is thrown and then use that knowledge to call the third party object correctly.

    Just an idea.

     

    p.s. Bah!!


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks

    • Edited by Derek Smyth Tuesday, December 13, 2011 2:34 PM
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:33 PM
  • Is it a StackOverflowException? You can't catch or recover from that.

    If you have:

    try { DoThirdPartyCall(); } catch (Exception) {}
    

    And the exception is not caught by that, you've got an exception that is not possible to catch or recover from, such as the StackOverflowException.


    Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:43 PM
  • Hi Mehmood,

    Every exception has got a type.If yes, then one thing you can do is , as you metioned in your previous code snippet...I have modified this a bit..Hope this helps...

    try { try { AddThirdPartyObject(); } catch (Exception ex) { if(ex is ThirdPartyException) continue; // <-- Here i presume you are in a loop   } } catch (Exception) { }

    Well , as every other person is suggesting here that swallowing an exception is not a good idea, but as one of the person suggested that , find the cause of it because if you find the cause then you can do the same thing in a different way and you won't get this particular exception which is nice anc cleaner way of avoiding exception rather than swallowing it.

    I hope this helps.

    Happy Coding

    Thanks,

    BR,

    JollyWagoner

    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 3:44 PM
  • Embedd all your code in a try block and put the final statements in a finally block.
    João Miguel
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 3:57 PM
  • An exception raised in another thread wouldn't be caught either.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 4:14 PM
  • So you're trying to handle an unhandled exception yet you get another one that your own control throws ?


    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Wednesday, December 14, 2011 7:56 AM

All replies

  • Although it's not a bad practice to ignore exceptions, I think you want the following:

    try
    {
    //...your code where you use the third party control
    }
    catch(Exception)
    {
    //...swallow exception
    }


     

    May I recommend to still add some logging (so in the catch-block, write to your log that the exception was thrown) and provide any feedback to the third party vendor so they can fix this issue.

     


    Dimitri C. - Please mark the replies as answers if they help! Thanks.
    • Edited by Dimitri C Tuesday, December 13, 2011 11:40 AM
    • Proposed as answer by JMCF125 Tuesday, December 13, 2011 3:55 PM
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:25 AM
    Tuesday, December 13, 2011 11:39 AM
  • Hello,

    The best way to swallow an exception is to either handle it or rethrow it!

    You shouldn't swallow exceptions, there's a good reason you get this exception, so the question should be about how you handle it rather than how you swallow it.


    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 11:40 AM
  • Thans Dimitri and eyal.

    i tried to swallow the exception but it is still raised

    try 
    { 
     
    } 
    catch (Exception ex) 
    { 
     
    } 
     
    ================== 
    Or in this way. 
     
    try 
    { 
    try 
    { 
     AddThirdPartyObject(); 
    } 
    catch (Exception ex) 
    { 
    } 
    } 
    catch (Exception) 
    { 
    

    maifs
    Tuesday, December 13, 2011 1:11 PM
  • What do you mean by still raised? The exception is not caught and the application is crashing?

    If yes, I guess AddThirdPartyObject() function is dealing with some unmanaged code which is failing (possibly memory corruption issue).


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 1:22 PM
  • hopefully it's not a corrupted state exception that you're trying to swallow.

    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:08 PM
  • You could find out why the exception is thrown and then use that knowledge to call the third party object correctly.

    Just an idea.

     

    p.s. Bah!!


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks

    • Edited by Derek Smyth Tuesday, December 13, 2011 2:34 PM
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:33 PM
  • Is it a StackOverflowException? You can't catch or recover from that.

    If you have:

    try { DoThirdPartyCall(); } catch (Exception) {}
    

    And the exception is not caught by that, you've got an exception that is not possible to catch or recover from, such as the StackOverflowException.


    Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 2:43 PM
  • Hi Mehmood,

    Every exception has got a type.If yes, then one thing you can do is , as you metioned in your previous code snippet...I have modified this a bit..Hope this helps...

    try { try { AddThirdPartyObject(); } catch (Exception ex) { if(ex is ThirdPartyException) continue; // <-- Here i presume you are in a loop   } } catch (Exception) { }

    Well , as every other person is suggesting here that swallowing an exception is not a good idea, but as one of the person suggested that , find the cause of it because if you find the cause then you can do the same thing in a different way and you won't get this particular exception which is nice anc cleaner way of avoiding exception rather than swallowing it.

    I hope this helps.

    Happy Coding

    Thanks,

    BR,

    JollyWagoner

    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 3:44 PM
  • Embedd all your code in a try block and put the final statements in a finally block.
    João Miguel
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 3:57 PM
  • An exception raised in another thread wouldn't be caught either.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Tuesday, December 13, 2011 4:14 PM
  • I mean excpetion couldn't be suppressed after write try catch block as you and i mentioned in above post.

    I debugged my source and came to know that after performing all actions of that silverlight window (in which i wrote the try catch block  for suppressing exception). Execution flow simply moves from child window to app.xamls.cs .

    namespace NFS.UI.Silverlight 
    { 
        using System; 
        using System.Runtime.Serialization; 
        using System.ServiceModel.DomainServices.Client.ApplicationServices; 
        using System.Windows; 
        using System.Windows.Controls; 
        using NFS.UI.Silverlight.Controls; 
        using NFS.UI.Silverlight.ServiceClient; 
        using System.Collections.ObjectModel; 
     
         
        public partial class App : Application 
        { 
            private System.Windows.Controls.BusyIndicator busyIndicator; 
     
          
            public App() 
            { 
                InitializeComponent();        
               
            } 
     
            private void Application_Startup(object sender, StartupEventArgs e) 
            { 
                 
                this.InitializeRootVisual(); 
            } 
     
          
            private void Application_UserLoaded(LoadUserOperation operation) 
            { 
            } 
            protected virtual void InitializeRootVisual() 
            { 
                this.busyIndicator = new System.Windows.Controls.BusyIndicator(); 
                this.busyIndicator.Content = new MasterPage(); 
                this.busyIndicator.HorizontalContentAlignment = HorizontalAlignment.Stretch; 
                this.busyIndicator.VerticalContentAlignment = VerticalAlignment.Stretch; 
     
                this.RootVisual = this.busyIndicator; 
            } 
     
            static ErrorWindow _errorWin = null; 
     
            private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) 
            { 
                if (!System.Diagnostics.Debugger.IsAttached) 
                { 
     
                } 
     
                e.Handled = true; 
     
                if (_errorWin == null || 
                    (_errorWin.DialogResult.HasValue && 
                    _errorWin.DialogResult.Value)) 
                    _errorWin = ErrorWindow.CreateNew(e.ExceptionObject); 
            } 
        } 
    }

       

    and at below line, it throws an exception

    _errorWin = ErrorWindow.CreateNew(e.ExceptionObject);

    and exception description is that

    Object reference not set to an instance of an object.

      at Telerik.Windows.Controls.GridView.GridViewCell.RaiseCellEditEndedEvent(GridViewEditAction editAction)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.PerformCellEditEnded(GridViewCell currentCell)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.CommitCellEdit(GridViewCell currentCell, Boolean isLeavingRow)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.CanCellBecomeCurrent(GridViewCell cell)
       at Telerik.Windows.Controls.GridView.GridViewCell.OnMouseLeftButtonDown(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)


    maifs
    Wednesday, December 14, 2011 7:27 AM
  • So you're trying to handle an unhandled exception yet you get another one that your own control throws ?


    Eyal (http://shilony.net), Regards.
    • Marked as answer by Mehmood Ahmed Wednesday, December 14, 2011 10:26 AM
    Wednesday, December 14, 2011 7:56 AM