如何在函数的c#中使用异常? [英] How to hanlde Exception in c# for a function?

查看:84
本文介绍了如何在函数的c#中使用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在函数的c#中使用异常?

解决方案

通常,处理它们for a function通常是一个坏主意,也是本地的。应该很少发现例外情况。它们跳过堆栈帧,这超出了通常的调用/返回机制。该技术旨在将常规指令流与异常情况及其更正隔离开来。



您可以在我过去的答案中找到一些主要想法:

C#构造函数中的异常会导致来电者分配失败吗? [ ^ ],

操作系统中存储.net异常的位置 [< a href =http://www.codeproject.com/Answers/700661/where-was-stored-net-exceptions-in-operating-syste#answer1target =_ blanktitle =New Window> ^ ]。



一些更具体的想法:

从网络摄像头拍摄的图像未保存在服务器上 [ ^ ],

异常详细信息:System.Runtime.InteropServices.COMException:使用CLSID检索组件的COM类工厂{0006F03A-0000-0000-C000-000000000046}由于以下错误而失败:... [ ^ ],

处理类库(dll)中的异常 [ ^ ],

扔。 .then ... rethrowing [ ^ ],

当我运行一个应用程序时,会遇到如何处理这个问题的异常? [ ^ ],

保持ac#windows表单应用程序运行,尽管有任何未处理的异常 [ ^ ],

我如何制作一个循环tha当滚动条到达底部时,t将停止 [ ^ ]。



-SA


你可以使用try ... catch语句。



 尝试 
{
// 做点什么
}
catch (例外e)
{
// 此处处理异常
}
最后
{
// 此块可以忽略
}


How to hanlde Exception in c# for a function?

解决方案

Usually, handling them "for a function" is usually a bad idea, too locally. Exception should be rarely caught. They "jump" over the stack frames, which is beyond the usual call/return mechanism. The technology is created to isolate "regular" instruction flow from exceptional situations and their corrections.

You can find some main ideas in my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

Some of more specific ideas:
Image taken from Webcam not getting saved on the server[^],
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^],
Handling exceptions in class library (dll)[^],
throw . .then ... rethrowing[^],
When i run an application an exception is caught how to handle this?[^],
Keep a c# windows form application running despite any unhandled exception[^],
How do i make a loop that will stop when a scrollbar reaches the bottom[^].

—SA


You can use try ... catch statement.

try
{
 // do something
}
catch(Exception e)
{
// deal exception here
}
finally
{
  // this block cab be ignored
}


这篇关于如何在函数的c#中使用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆