예제 #1
1
파일: Program.cs 프로젝트: zaekeon/csharp2
        static void Main(string[] args)
        {

            //there's a cleaner way to write this code...using the 'using' keyword.
            
            //exceptions should be from most specific to most generic..
            StreamReader stream = null;  //have to initialize it to null or you can't use it in the try block...
            try
            {
                Calculator calc = new Calculator();
                calc.Divide(5, 0);
               stream  = new StreamReader(@"c:\file.zip");
                var content = stream.ReadToEnd();
            }

            catch(DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
            }

            catch (ArithmeticException ex)
            {
                Console.WriteLine(ex.Message);
            }

            catch (Exception ex)
            {

                Console.WriteLine("Sorry an error occured");
               // throw;  //throw can send the error to the caller of the code.
            }
            finally  //finally will run.
            {
                //unmanaged resources need to implement IDisposable interface.
                Console.WriteLine("The bacon flows from within");
                if (stream != null)
                stream.Dispose();
            }





        }
        static void Main(string[] args)
        {
            try
            {
                var api = new YoutubeApi();
                var video = api.GetVideo("Mosh");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                var calculator = new Calculator();
                var result = calculator.Divide(5, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            #region One

            try
            {
                var calculator = new Calculator();
                var result     = calculator.Divide(5, 0);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("You cannot divide by 0.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sorry, an unexpected error occured");
            }

            #endregion

            Console.WriteLine();
            Console.WriteLine("*******************************");
            Console.WriteLine("*******************************");

            #region Two

            StreamReader streamReader = null;

            try
            {
                streamReader = new StreamReader(@"c:\file.zip");
                var content = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sorry, an unexpected error occured");
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Dispose();
                }
            }

            #endregion

            Console.WriteLine();
            Console.WriteLine("*******************************");
            Console.WriteLine("*******************************");

            #region Three

            try
            {
                using (var streamRead = new StreamReader(@"c:\file.zip"))
                {
                    var content = streamRead.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops");
            }

            #endregion

            Console.WriteLine();
            Console.WriteLine("*******************************");
            Console.WriteLine("*******************************");

            #region Four

            try
            {
                var api    = new YoutubeApi();
                var videos = api.GetVideos("");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            #endregion
            Console.ReadLine();
        }