异常处理通过 try、catch、finally 和 throw 模型进行管理。这些是整个模型中使用的关键字。下面是每个关键字的解释:
Try:我们将代码保存在我们想要处理异常的 try 块中。
Catch:当 try 块中发生任何异常时,在异常处理程序的帮助下将其捕获到 catch 块中。
finally:为了不考虑错误地执行代码块,我们将该代码放在 finally 块中以执行。
Throw:发生问题时抛出异常。
下面是异常处理的例子:
public class SomeClass { public void GetData() { try { //write some code here } catch (Exception) { throw; } finally { /*code to execute in the last like dispose objects and resource*/ } } }
异常处理通过 try、catch、finally 和 throw 模型进行管理。这些是整个模型中使用的关键字。下面是每个关键字的解释:
Try:我们将代码保存在我们想要处理异常的 try 块中。
Catch:当 try 块中发生任何异常时,在异常处理程序的帮助下将其捕获到 catch 块中。
finally:为了不考虑错误地执行代码块,我们将该代码放在 finally 块中以执行。
Throw:发生问题时抛出异常。
下面是异常处理的例子: