Beispiel #1
0
        static void Main(string[] args)
        {
            //try
            //{
            //    using (var streamReader = new StreamReader(@"c:\file.zip"))
            //    {
            //        var content = streamReader.ReadToEnd();
            //    }
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine("Unexpected error occurred");
            //}
            //finally
            //{
            //    ////IDisposable
            //    //if(streamReader != null)
            //    //    streamReader.Dispose();
            //}

            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("Zane");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            #region StreamReader Example
            // StreamReader streamReader = null;
            try
            {
                // using statement here allows us to not need to dispose, and thus not need the finally block
                using (var streamReader = new StreamReader(@"C:\Personal Projects\Udemy\C-Sharp-Intermediate-Skills\log.txt"))
                {
                    var content = streamReader.ReadToEnd();
                    Console.WriteLine(content);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Sorry, an unexpected error ocurred: {ex.Message}");
            }
            //finally // Finally blocks are always excuted, regardless of whether or not an exception is thrown.
            //{
            //    if (streamReader != null)
            //        streamReader.Dispose(); // prevents us from accidentally leaving a file open on the disk, leaving network or db connections open, and overloading resources
            //}
            #endregion

            #region YouTubeApi Example
            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("ransford");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " Inner Execption: " + ex.InnerException.Message);
            }

            #endregion

            #region Caclulator
            try
            {
                var calc   = new Calculator();
                var result = calc.Divide(5, 0);
            }
            catch (DivideByZeroException ex) { Console.WriteLine("You cannont divide by zero."); }
            catch (ArithmeticException ex) { Console.WriteLine("Your Maths were bad."); }
            catch (Exception ex) // 'Exception' is the parent of all exceptions in C#
            {
                Console.WriteLine($"Sorry, an unexpected error occured: {ex.Message}");
            }
            finally
            {
            }
            #endregion
        }
Beispiel #3
0
 static void Main(string[] args)
 {
     try
     {
         var api    = new YouTubeApi();
         var videos = api.GetVideos("mosh");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Beispiel #4
0
 private static void GetVideos()
 {
     try
     {
         var api = new YouTubeApi();
         api.GetVideos("Michiel");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Beispiel #5
0
 static void Main(string[] args)
 {
     try
     {
         Console.WriteLine("This is a test branch version.");
         var youTubeApi = new YouTubeApi();
         youTubeApi.GetVideos("Peti");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Beispiel #6
0
        public static void Main(string[] args)
        {
            // Below is the simple calculator example
            //try
            //{
            //    var calculator = new Calculator();
            //    var result = calculator.Divide(5, 0);

            //    Console.WriteLine($"The result: {result}");
            //}
            //catch (DivideByZeroException ex)
            //{
            //    Console.WriteLine("You cannot divide by zero.");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry an unexspected exception occurred.");
            //}

            //Below is the StreamReader exception example
            //StreamReader reader = null;

            //try
            //{
            //    reader = new StreamReader(@"C:\Documents\sampletext.txt");
            //    var text = reader.ReadToEnd();

            //   throw new Exception("An exception has occured!");

            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine($"A problem has occured : {ex.Message}.");
            //}
            //finally
            //{
            //    if (reader != null)
            //        reader.Dispose();
            //}

            // Creating a YouTube exception
            try
            {
                var youTubeApi = new YouTubeApi();
                youTubeApi.GetUserVideos();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message} - {ex.InnerException.Message}");
            }
        }
Beispiel #7
0
 static void Main(string[] args)
 {
     try
     {
         var video = new YouTubeApi().GetVideos("Shefali");
         //using (StreamReader streamReader = new StreamReader(""))
         //{
         //    var video =
         //}
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
     Console.ReadLine();
 }
        static void Main(string[] args)
        {
            //try
            //{
            //    var calculator = new Calculator();
            //    var result = calculator.Divide(5, 0);
            //}
            //catch (DivideByZeroException ex)
            //{
            //    Console.WriteLine("You cannot divide by zero.");
            //}
            //catch (ArithmeticException ex)
            //{

            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    //Dispose için kullan
            //}

            //Bu şekilde dışarıda değişkeni tanımlayıp finally ile yakalamak yerine using kullanabiliriz.
            //StreamReader streamReader = null;
            //try
            //{
            //    streamReader = new StreamReader(@"C:\file.zip");
            //    streamReader.ReadToEnd();
            //    throw new Exception("Oops");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    if (streamReader != null)
            //        streamReader.Dispose();
            //}

            //using ile kullanımı
            //try
            //{
            //    using (var streamReader = new StreamReader(@"C:\file.zip"))
            //    {
            //        var content = streamReader.ReadToEnd();
            //    }
            //}
            //catch (Exception ex)//Global excepttion handler
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}

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

            Console.ReadLine();
        }
Beispiel #9
0
        static void Main(string[] args)
        {
//            try
//            {
//                var calculator = new Calculator();
//                var result = calculator.Divide(5, 0);
//            }
//
//            catch (DivideByZeroException ex)
//            {
//
//            }
//
//            catch (ArithmeticException ex)
//            {
//
//            }
//            catch (Exception ex)
//            {
//                Console.WriteLine("Sorry, an unexpected error occurred.");
//            }
//            finally
//            {
//                IDisposable
//            }

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



//            try
//            {
//                using (var streamReader = new StreamReader(@"C:\file.zip"))
//                {
//                var content = streamReader.ReadToEnd();
//                }
//            }
//            catch (Exception ex)
//            {
//                Console.WriteLine("Sorry, an unexpected error occurred.");
//            }

            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("user");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            //try
            //{
            //    var calculator = new Calculator();
            //    var result = calculator.Divide(5, 0);


            //}
            //// adding the variable ex gives access to the information one gets by hovering over ex in VS
            //catch (DivideByZeroException ex)  // multiple catch block must be in specific to generic order
            //{
            //    Console.WriteLine("You cannot divide by 0.");
            //}
            //catch (ArithmeticException ex)  // otherwise the generic will fire, and we won't see the specific one
            //{

            //}
            //catch (Exception ex)    // It's good to have an argument here to access actual exception
            //{
            //    Console.WriteLine("Sorry, an unexpected error occurred.");
            //}
            //finally
            //{
            //}

            //StreamReader streamReader = null;
            //try
            //{

            //    streamReader = new StreamReader(@"c:\file.zip");
            //    var content = streamReader.ReadToEnd();
            //    throw new Exception("Oops");      // one can just manually throw an exception
            //}
            //catch (Exception)   // Only executed if there is a problem in the try block
            //{
            //    Console.WriteLine("Sorry, an unexpected error occurred.");
            //}
            //finally
            //{
            //    if (streamReader != null)
            //        streamReader.Dispose();     // executed no matter what calls dispose on streamReader to close file handle
            //}

            // A better way of doing the above is this (cleaner)
            //try
            //{
            //    using (var streamReader = new StreamReader(@"c:\file.zip"))  // using will automatically call dispose at the end
            //    {
            //        var content = streamReader.ReadToEnd();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occurred.");
            //}

            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("mosh");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);      // The message created in the other methods gets passed here
            }                                       // This is the only cw
        }
Beispiel #11
0
        public static void Main(string[] args)
        {
            // Example showing multiple catch blocks with decreasingly specific Exceptions
            try
            {
                var calculator = new Calculator();
                var result     = calculator.Divide(5, 0);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Divide by Zero");
            }
            catch (ArithmeticException ex)
            {
                Console.WriteLine("Arith exception");
            }
            catch (Exception ex)
            {
                Console.WriteLine("An unexpected error occurred");
            }
            finally
            {
            }

            // Another example showing the finally block being used
            StreamReader streamReader = null;

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

            // This can be cleaned up with the "using" statement, which creates a "finally" block under the hood:
            try
            {
                using (var streamReaderClean = new StreamReader(@"c:\file.zip"))
                {
                    var content = streamReaderClean.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An unexpected error occurred");
            }

            // Custom exceptions
            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("mark");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            try
            {
                var calculator = new Calculator();
                var result     = calculator.Divide(5, 0);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine("Sorry, an unexpected divide by zero error occurred.");
            }
            catch (ArithmeticException ex)
            {
                Console.WriteLine("Sorry, an unexpected arithmetic error occurred.");
            }
            catch (SystemException ex)
            {
                Console.WriteLine("Sorry, an unexpected system error occurred.");
            }
            catch (Exception ex) //variable for exception not required just the Exception class but by having the variable you can call it later
            {
                Console.WriteLine("Sorry, an unexpected error occurred.");
            }
            finally //great for cleaning up unmanaged resources
            {
                Console.WriteLine("Finally.");
            }

            //dynamic streamReader = 0;
            StreamReader streamReader = null;

            Console.WriteLine("Test");
            try
            {
                streamReader = new StreamReader(@"c:\file.zip");
                var content = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sorry, an unexpected error occurred.");
            }
            finally //great for cleaning up unmanaged resources
            {
                if (streamReader != null)
                {
                    streamReader.Dispose();
                    Console.WriteLine("Successfully Disposed!");
                }
            }

            try
            {
                using (var streamReader1 = new StreamReader(@"c:\file.zip")) //automatically sets up hidden finally and dispose this is the prefferable method
                {
                    var content = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sorry, an unexpected error occurred.");
            }


            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("mosh");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            //var filePath =
            //    @"C:\Users\Nilton\Documents\GitHub\udemy-c-sharp-advanced-topics\section02\ExceptionHandling\file.txt";
            //var streamReader = new StreamReader(filePath);
            //try
            //{
            //    var calculator = new Calculator();
            //    var result = calculator.Divide(5, 0);
            //}
            //catch (DivideByZeroException ex)
            //{
            //    Console.WriteLine("You cannot divide by 0.");
            //}
            //catch (ArithmeticException ex)
            //{

            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    streamReader.Dispose();
            //}

            //StreamReader streamReader = null;
            //var filePath =
            //    @"C:\Users\Nilton\Documents\GitHub\udemy-c-sharp-advanced-topics\section02\ExceptionHandling\file.txt";
            //try
            //{
            //    streamReader = new StreamReader(filePath);
            //    var content = streamReader.ReadToEnd();
            //    throw new Exception("Oops");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    if (streamReader != null)
            //        streamReader.Dispose();
            //}

            //var filePath =
            //    @"C:\Users\Nilton\Documents\GitHub\udemy-c-sharp-advanced-topics\section02\ExceptionHandling\file.txt";
            //try
            //{
            //    using (var streamReader = new StreamReader(filePath))
            //    {
            //        var content = streamReader.ReadToEnd();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}


            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("mosh");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            StreamReader streamReader = null;

            // below is global exception error handling block: the exceptions are from the most specific one to the most generic exception

            // defined a stream reader, stream reader is a class for reading any streams of data(files, network, etc)


            try
            {
                // option 2: use using method: using method will create the finally block under the hook
                // so you do not have to create finally block for using() method
                using (var streamReader0 = new StreamReader(@"c:\file.zip"))
                {
                    var content0 = streamReader0.ReadToEnd();
                }


                // option 1: {
                streamReader = new StreamReader(@"c:\file.zip");

                // in try block, we can read the content of the this file
                var content = streamReader.ReadToEnd();
                throw new Exception("opps");


                // } option 1



                // option calculator {
                Calculator calculator = new Calculator();
                calculator.Divide(5, 0);
                // } option calculator
            }
            // catch the errors from the most specific to the most generic
            catch (DivideByZeroException ex)    // DivideByZeroException class
            // if you are using Exception class, it is a parent of all of Excepts
            {
                Console.WriteLine("You can not divide by 0.");
            }
            catch (ArithmeticException ex) // ex is an argument
            {
                Console.WriteLine("second level exception error.");
            }
            catch (Exception ex)
            {
                // 1. recover from the error
                Console.WriteLine("An unexpected error occurred.");
                // 2. re-throw the error
            }
            finally // finally block is not applied to using method in try block

            // finally block, in .Net there are some classes are access unmanaged resources,
            //which these resources are not managed by CLR, it means there is no garbage collection to apply to them.
            //it means we need to manually clean out, by using IDisposable interface, and a method call Dispose()
            // we use finally to call IDisposable method for clean out unmanaged resources


            {
                if (streamReader != null)
                {
                    streamReader.Dispose();
                    // if anything goes wrong inside the try block during reading the file, we want to make sure streamReader is closed
                    // if we do not Dispose streamReader, it will keep the file opened on the disk, keep database opens, or keep network opens

                    // so whenever using unmanaged resource, make sure to dispose in the finally block
                }


                //     Console.WriteLine("Finally block is to call the <dispose method> of the class that is using the manage resources");
            }



            // For YouTubeAPI class
            try
            {
                var api    = new YouTubeApi();
                var videos = api.GetVideos("mosh");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            //try
            //{
            //    var calculator = new Calculator();
            //    var result = calculator.Divide(5, 0);
            //}
            //catch (DivideByZeroException ex)
            //{
            //    Console.WriteLine("You cannot divide by zero.");
            //}
            //catch (ArithmeticException ex)
            //{

            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    //Dispose için kullan
            //}


            //Bu şekilde dışarıda değişkeni tanımlayıp finally ile yakalamak yerine using kullanabiliriz.
            //StreamReader streamReader = null;
            //try
            //{
            //    streamReader = new StreamReader(@"C:\file.zip");
            //    streamReader.ReadToEnd();
            //    throw new Exception("Oops");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}
            //finally
            //{
            //    if (streamReader != null)
            //        streamReader.Dispose();
            //}

            //using ile kullanımı
            //try
            //{
            //    using (var streamReader = new StreamReader(@"C:\file.zip"))
            //    {
            //        var content = streamReader.ReadToEnd();
            //    }
            //}
            //catch (Exception ex)//Global excepttion handler
            //{
            //    Console.WriteLine("Sorry, an unexpected error occured.");
            //}


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

            Console.ReadLine();
        }