none
Try-catch doesn't catch interop exception RRS feed

  • Question

  • With .NET Framework v2.0, if Photoshop isn't installed on the user's system when my code attempts to open it, this .NET runtime exception is thrown:

    Microsoft .NET Framework
    Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.
    Retrieving the COM class factory for component with CLSID {B827BBBB-1621-4064-812B-0D759D04A008} failed due to the following error: 80040154.

    I've placed the code to open Photoshop in a try block and a user-friendly explanatory message box in the catch block. The exception isn't caught and the user sees the .NET runtime exception message.

    The application works fine when the third party software is installed.

    How do I catch this exception?
    Alternatively, how do I detect the presence/absence of Photoshop on the user's system so I can avoid the exception.

    Thanks for your help.
    Gus

    Monday, February 12, 2007 2:24 AM

Answers

  • Are you sure this is where the error is occurring. In some circumstances COM will error when the first method is called on the created object, rather than where the object is created, although I wouldn't have thought so here.

    If you go to Debug | Exceptions and check the Thrown box next to "Common Language Runtime Exceptions" your code should break at the exact location of the error.

    Monday, February 12, 2007 9:18 PM

All replies

  • Actually, catch should trap any interop exceptions. Can you post the catch block?

    As for testing for Photoshop, if you look in the Registry there should be a key under HKEY_LOCAL_MACHINE\SOFTWARE\Abode\Photoshop. I say should, since I don't have photoshop, but that's the pattern that most applications use.

    Monday, February 12, 2007 5:42 AM
  • Here is the relevant code.

    Private ps As Photoshop.Application

    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    ...

           Try
     
                ps = New Photoshop.Application   ' Start Photoshop

            Catch ex As Exception

                MessageBox.Show("Photoshop is not installed.", "Photoshop not installed", MessageBoxButtons.OK)

                Exit Sub

            End Try

     ...

    End Sub

    Thanks, Gus

    Monday, February 12, 2007 8:04 PM
  • Are you sure this is where the error is occurring. In some circumstances COM will error when the first method is called on the created object, rather than where the object is created, although I wouldn't have thought so here.

    If you go to Debug | Exceptions and check the Thrown box next to "Common Language Runtime Exceptions" your code should break at the exact location of the error.

    Monday, February 12, 2007 9:18 PM
  • Your hunch is correct. I didn't have time to search for the exact statement (would have had to uninstall/reinstall Photoshop with the accompanying hassle) so I placed the entire procedure in the try block and the exception was caught. Later I will find out what exactly caused the error, but for the moment a solution is at hand.

    Thank you Sean. Very much.

    Gus

    Tuesday, February 13, 2007 4:53 AM