Resolved it doesnt work anymore

CBruns

New member
Joined
Sep 2, 2020
Messages
3
Programming Experience
Beginner
It worked 1 Hour ago and yet when i am home it doesnt work anymore
It should throw an exception whenever someone typed in 0 so that he can renew it now it divides by i dont know what and the same anwser is 8 all the time when i want to divide by zero
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exception2
{
    class Program
    {
        private const string Value = "Sie haben versucht mit null zu dividieren die zahl wird jetzt automatisch auf 1 gesetzt";

        static void Main(string[] args)
        {
            double zahl1 = 0;
            double zahl2 = 0;
            double zahl3 = 0;

            Console.WriteLine("Bitte geben sie eine Zahl ein");
            zahl1 = Convert.ToDouble(Console.ReadLine());

            try
            {
                Console.WriteLine("Bitte geben sie eine Zweite Zahl ein die nicht NULL oder 0 ist");
                zahl2 = Convert.ToDouble(Console.ReadLine());
            }
            catch (Exception l)
            {
                Console.WriteLine(l.Message);
                Console.WriteLine(Value);
                zahl2 = 1;
                Console.ReadKey();             
            }

            try
            {
                zahl3 = zahl1 / zahl2;
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("Bitte geben sie eine zahl ein die NICHT 0 ist");

                zahl2 = Convert.ToDouble(Console.ReadLine());

                if (zahl2 == 0)
                {
                    Console.WriteLine("Ich habe gesagt NICHT durch NULL 0 du Vollidiot");
                    Console.WriteLine("Das Programm wird sich jetzt selber schließen");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
                if (zahl2 != 0)
                {
                }
            }

            Console.WriteLine("Der Quotient ist" + " " + zahl3);
            Console.ReadKey();
        }
    }
}
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
So, when you debugged the code, i.e. set a breakpoint at the top of the code, stepped through it line by line and tested the state at each step, EXACTLY where and how did the reality differ from your expectations?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,786
Location
Chesapeake, VA
Programming Experience
10+
In general, you should not be using exceptions for flow control. Check for zero before trying to divide, instead of trying to divide and then catching the exception.

The same is true for your taking in your inputs. You should use TryParse() instead of converting and then catching an exception.
 

CBruns

New member
Joined
Sep 2, 2020
Messages
3
Programming Experience
Beginner
i am a beginner so i dont know that much about c#. and what i sent in was the code from the Teacher that was written on the board. in the school it worked on the PCs and on my home systems it doesnt work that is my problem
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,786
Location
Chesapeake, VA
Programming Experience
10+
The DivideByZero exception is only thrown for integral types and the decimal type. You are getting the expected infinity output since your types are double.

 

CBruns

New member
Joined
Sep 2, 2020
Messages
3
Programming Experience
Beginner
So, when you debugged the code, i.e. set a breakpoint at the top of the code, stepped through it line by line and tested the state at each step, EXACTLY where and how did the reality differ from your expectations?
It doesnt goes into the exception when i want to type in the zero
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
It doesnt goes into the exception when i want to type in the zero
That's really way too vague. You need to be specific. You need to provide something along the lines of:
At line X, the values of variables Y and Z are A and B and I expect C to happen but what actually happens is D.
It's not hard to provide a FULL and CLEAR explanation if you take the time to think about what you're doing, what you expect and what actually happens.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,786
Location
Chesapeake, VA
Programming Experience
10+
The code is going from line 37 to line 57. The OP was expecting the code to go to line 41.
Screenshot_1.png
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
The code is going from line 37 to line 57. The OP was expecting the code to go to line 41.
I could have worked that out for myself with a bit of effort but the point is that it shouldn't be for us to each make that effort to work out something that the OP already knows or explain things to each other that the OP should explain to all. This issue has already been resolved so it's not specifically relevant here but I'm trying to get the OP to do better in future, for everyone's sake.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,786
Location
Chesapeake, VA
Programming Experience
10+
Top Bottom