Example #1
0
        static DivideMethodException ConvertToCustomException(Exception ex,
                                                              string message = null)
        {
            DivideMethodException divEx = null;

            if (message != null)
            {
                divEx = new DivideMethodException(message, ex);
            }
            else
            {
                divEx = new DivideMethodException(ex.Message, ex);
            }

            return(divEx);
        }
Example #2
0
        static int Divide()
        {
            //int x = 10;
            //int y = 0;
            //int res = 0;
            StreamWriter writer = null;

            try
            {
                writer = new StreamWriter(@"data.txt");
                writer.WriteLine("hello");
                return(1);
                //res = x / y;
                //return res;
            }
            catch (DivideByZeroException ex)
            {
                DivideMethodException divEx = ConvertToCustomException(ex, "Denominantor should not be zero...");
                throw divEx;
            }
            catch (IndexOutOfRangeException ex)
            {
                DivideMethodException divEx = ConvertToCustomException(ex);
                throw ex;
            }
            catch (Exception ex)
            {
                DivideMethodException divEx = ConvertToCustomException(ex);
                throw ex;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Flush();
                    writer.Close();
                }
            }
        }