欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中的try catch finally用法分析

程序员文章站 2023-12-14 14:54:16
本文实例讲述了c#中的try catch finally用法。分享给大家供大家参考。具体分析如下: try中的程序块是有可能发生错误的程序块,catch中的程序块是当发生...

本文实例讲述了c#中的try catch finally用法。分享给大家供大家参考。具体分析如下:

try中的程序块是有可能发生错误的程序块,catch中的程序块是当发生错误的时候才会执行的代码块,finally中的程序块是无论是否发生错误都会执行的代码块。

示例程序:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
 
namespace sample_01_ca
{
    public class complex
    {
        static void main(string[] args)
        {
            int i = 2008;
            int j = 0;
            try
            {
                int result = i / j;
            }
            catch
            {
                console.writeline("j变量的值为0");
            }
            finally
            {
                console.writeline(j.tostring());
            }
            console.read();
        }
    };
}

程序输出为:

j变量的值为0

0

示例程序:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
 
namespace sample_01_ca
{
    public class complex
    {
        static void main(string[] args)
        {
            int i = 2008;
            int j = 2;
            try
            {
                int result = i / j;
            }
            catch
            {
                console.writeline("j变量的值为0");
            }
            finally
            {
                console.writeline(j.tostring());
            }
            console.read();
        }
    };
}

程序输出为:

0

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: