首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何忽略异常

如何忽略异常
EN

Stack Overflow用户
提问于 2012-11-14 01:20:52
回答 2查看 470关注 0票数 1

我正在用C#编写一个kinect应用程序,我有以下代码

代码语言:javascript
复制
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所做的更改)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-14 01:28:21

目前,您正在捕获ComExceptions的所有内容。如果要捕获其他异常,则需要为每个异常提供特定的类型。

您可以将异常类型添加到catch块之后,如下所示:

代码语言:javascript
复制
    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

像这样

代码语言:javascript
复制
try
{
  //logic
}
finally
{
  //logic. This will be executed and then the exception will be catched
}
票数 1
EN

Stack Overflow用户

发布于 2012-11-14 01:27:45

如果您想忽略所有异常:

代码语言:javascript
复制
try 
{
// your code... 
} 
catch (Exception E)
{ 
// whatever you need to do...
};

上面的代码是包罗万象的(尽管有些异常不能像Stackoverflow那样被捕获)。

备注

你不应该使用上面的代码...您应该找出抛出的是哪种类型的异常,并捕获它!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13365513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档