新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C#快餐-8 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2388 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C#快餐-8 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 C#快餐-8


    发信人: nice (春天), 信区: DotNET        
    标  题: C#快餐-8
    发信站: BBS 水木清华站 (Thu Jul 26 02:09:18 2001)

    Lesson 8.  Exceptions


        There are no exceptions in C and in C++ one can get away from using  
    them with error handling functions such as exit() and terminate(). In C#  
    these functions are absent and we introduce exceptions which take their place.
    When a program has a bug we can intercept it in the flow of execution by  
    inserting an error handling statement. For example let's say we have a program
    which does not accesses the first element of an array. Then, during some array
    handling operations, if an access to array[0] is attempted, the OutOfRange
    exception is thrown. class OutOfRange  is an exception class. All exception  
    classes inherit from class. Compiler will give an error if the inheritance  
    from the Exception class is not made. In the code that follows we throw an  
    exception OutOfRange if i == 0 is true. The try statement is enclosing the  
    part of the code where the exception is expected. If the exception occurs it  
    is handled by the code inside the catch statement. The catch statement  takes  
    as an argument an instance of class System.Exception.

    //first exception handling program
    using System;
    class OutOfRange: Exception{
    }
    class Demo
    {
        int n;
        public int []array;
        public Demo ( int n)
        {
            this.array = new int[n];
            this.n = n;
        }
        public void show_element (int i)
        {
            try
            {
                if (i == 0) throw ( new OutOfRange());
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception : {0}", e);
            }
            Console.WriteLine (array [i]);
        }
    }
    class Test
    {
        public static void Main()
        {
            Demo test = new  Demo (3);
            test.array [1] = 2;
            test.array [2] = 3;
            test.show_element (0);
        }
    }

    The output is :
    Exception : OutOfRange: An exception of type OutOfRange was thrown. at Demo.
    show_element(Int32 i) in G:\CSharpEd\excep.cs:line 19
    0

    The previous program can be simplified if you use a built in Exception class
    provided by the C# runtime. The full list of built in Exception classes  
    hasn't been published yet by Microsoft at the time of this writing.
    OutOfMemoryException  Thrown when an allocation of  memory  fails.
    StackOverflowException  Infinite recursion.
    IndexOutOfRangeException  Out of bounds access to an array
    ArithmeticException  A base class for exceptions DivideByZeroException and O
    verflowException
    OverflowException  Overlflow
    DivideByZeroException  Division by zero
    In the following program I use built in ArgumentException instead of  
    OutOfRange exception.

    //a better way to handle an Argument exception
    using System;
    class Demo
    {
        int n;
        public int []array;
        public Demo (int n)
        {
            this .array = new int [n];
            this .n = n;
        }
        public void show_element (int i)
        {
            try
            {
                if (i == 0) throw new ArgumentException ("Out of rannge");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Exception : {0}", e);
                return ;
            }
            Console.WriteLine (array [i]);
        }
    }
    class Test
    {
        public static void Main()
        {
            Demo test = new Demo(3);
            test.array [1] = 2;
            test.array [2] = 3;
            test.show_element (0);
        }
    }

    Write a simple program which divides two integers and try using DivideByZero
    Exception exception. Good Luck!

    --

    ※ 修改:·walts 於 Jul 26 10:10:41 修改本文·[FROM: 166.111.142.118]
    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.176.234]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/3 8:50:10

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms