我正在用C#编写一个kinect应用程序,我有以下代码
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;
}我正在寻找一种方法,让我的应用程序在kinect未连接时不会崩溃(例外情况被忽略)。我创建了另一个问题here,但解决方案无法应用于我的场合,因为我被迫使用过时的sdk,没有人可以解决这个问题,所以我尝试使用不同的方法。如何忽略此异常?(之后我可以自己撤销对_nui所做的更改)
发布于 2012-11-14 01:28:21
目前,您正在捕获ComExceptions的所有内容。如果要捕获其他异常,则需要为每个异常提供特定的类型。
您可以将异常类型添加到catch块之后,如下所示:
catch (System.Runtime.InteropServices.COMException)
{
MessageBox.Show("Could not initialize Kinect device.\nExiting application.");
_nui = null;
} catch (Exception ex) //this will catch generic exceptions.
{
} 如果您希望您的代码在catch之后执行,无论发生什么。您也可以尝试使用finally
像这样
try
{
//logic
}
finally
{
//logic. This will be executed and then the exception will be catched
}发布于 2012-11-14 01:27:45
如果您想忽略所有异常:
try
{
// your code...
}
catch (Exception E)
{
// whatever you need to do...
};上面的代码是包罗万象的(尽管有些异常不能像Stackoverflow那样被捕获)。
备注
你不应该使用上面的代码...您应该找出抛出的是哪种类型的异常,并捕获它!
https://stackoverflow.com/questions/13365513
复制相似问题