C#trycatch语句
在C#编程中,异常处理由try/catch语句执行。C#中的try block用于放置可能引发异常的代码。catch block用于处理异常。
不带try/catch的C#示例
using System; public class ExExample { public static void Main(string[] args) { int a = 10; int b = 0; int x = a/b; Console.WriteLine("Rest of the code"); } }
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
C#try/catch示例
using System; public class ExExample { public static void Main(string[] args) { try { int a = 10; int b = 0; int x = a / b; } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Rest of the code"); } }
System.DivideByZeroException: Attempted to divide by zero. Rest of the code
祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)
精选教程推荐
👇 以下精选教程可能对您有帮助,拓展您的技术视野
暂无学习笔记,成为第一个分享的人吧!
您的笔记将帮助成千上万的学习者