Question

I am writing a kinect app in C# and I have this code

try             //start of kinect code
{
    _nui = new Runtime();
    _nui.Initialize(RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseColor);

    // hook up our events for video
    _nui.DepthFrameReady += _nui_DepthFrameReady;
    _nui.VideoFrameReady += _nui_VideoFrameReady;

    // hook up our events for skeleton
    _nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(_nui_SkeletonFrameReady);

    // open the video stream at the proper resolution
    _nui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
    _nui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);

    // parameters used to smooth the skeleton data
    _nui.SkeletonEngine.TransformSmooth = true;
    TransformSmoothParameters parameters = new TransformSmoothParameters();
    parameters.Smoothing = 0.8f;
    parameters.Correction = 0.2f;
    parameters.Prediction = 0.2f;
    parameters.JitterRadius = 0.07f;
    parameters.MaxDeviationRadius = 0.4f;
    _nui.SkeletonEngine.SmoothParameters = parameters;

    //set camera angle
    _nui.NuiCamera.ElevationAngle = 17;
}
catch (System.Runtime.InteropServices.COMException)
{
    MessageBox.Show("Could not initialize Kinect device.\nExiting application.");
    _nui = null;

}

I am looking for a way for my app to not crash (the exception to be ignored) when kinect is not connected. I created another question here but the solutions could not be applied to my occasion as I am forced to use outdated sdk and nobody can solve that quesiton so I am trying to use a different approach. How can I ignore this exception? (I can reverse the changes made to _nui myself afterwards)

Was it helpful?

Solution

Currently you're catching all of ComExceptions. If you want to catch other exceptions, you need to provide specific types for each exception.

You can add your exception types after your catch block like this:

    catch (System.Runtime.InteropServices.COMException)
    {
        MessageBox.Show("Could not initialize Kinect device.\nExiting application.");
        _nui = null;

    } catch (Exception ex) //this will catch generic exceptions.
    {

    }  

If you want your code to execute after catch No matter what. you can also try to use finally

like this

try
{
  //logic
}
finally
{
  //logic. This will be executed and then the exception will be catched
}

OTHER TIPS

IF you want to ignore all exceptions:

try 
{
// your code... 
} 
catch (Exception E)
{ 
// whatever you need to do...
};

The above is a catch-all (although some exception can't be caught like Stackoverflow).

REMARK

You should NOT use the above... you should find out what sort of exception is thrown and catch that!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top