Exception handle with finally : Exception Finally « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Language Basics » Exception FinallyScreenshots 
Exception handle with finally
Exception handle with finally

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 namespace ExceptionHandling
 {
    public class TesterExceptionHandling6
    {
       public void Run()
       {
           try
           {
               Console.WriteLine("Open file here");
               double a = 12;
               double b = 0;
               Console.WriteLine ("{0} / {1} = {2}",
                   a, b, DoDivide(a,b));
               Console.WriteLine (
                   "This line may or may not print");
           }

               // most derived exception type first
           catch (System.DivideByZeroException e)
           {
               Console.WriteLine(
                   "\nDivideByZeroException! Msg: {0}",
                   e.Message);
               Console.WriteLine(
                   "\nHelpLink: {0}", e.HelpLink);
               Console.WriteLine(
                   "\nHere's a stack trace: {0}\n",
                   e.StackTrace);
           }
           catch
           {
               Console.WriteLine(
                   "Unknown exception caught");
           }
           finally
           {
               Console.WriteLine (
                   "Close file here.");
           }

       }

        // do the division if legal
        public double DoDivide(double a, double b)
        {
            if (b == 0)
            {
                DivideByZeroException e =
                    new DivideByZeroException();
                e.HelpLink =
                    "http://www.libertyassociates.com";
                throw e;
            }
            if (a == 0)
                throw new ArithmeticException();
            return a/b;
        }

        static void Main()
        {
            Console.WriteLine("Enter Main...");
            TesterExceptionHandling6 t = new TesterExceptionHandling6();
            t.Run();
            Console.WriteLine("Exit Main...");
        }
    }
 }
           
       
Related examples in the same category
1.Use final to deal with custom Exceptions
2.illustrates a try, catch, and finally blockillustrates a try, catch, and finally block
3.Demonstates the possible uses of a finally blockDemonstates the possible uses of a finally block
4.Exception with finallyException with finally
5.Use finallyUse finally
6.Exception Handling Finally
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.