|
-
Mar 29th, 2006, 09:55 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] divide by 0 exception
Hello guys, don't know why this code does not work. It does not compile at all
Code:
using System;
class division
{
int i;
int j;
public division (int i, int j)
{
this.i = i;
this.j = j;
}
public void divide(int n)
{
try
{
if (n/0) throw new DivideByZeroException("Divide by 0");
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception: {0}", e);
return;
}
Console.WriteLine(i / j);
}
}
class Test
{
public static void Main()
{
division d = new division(3, 1);
d.divide (2);
Console.ReadLine ();
}
}
-
Mar 29th, 2006, 10:03 PM
#2
Re: divide by 0 exception
Well it won't compile because you're explicitly dividing by zero, which is not possible, so why would the compiler even let you try. I assume that you meant to test whether n was equal to zero.
Apart from that, what purpose does the n argument serve? You're dividing i by j so it's j you should be testing for equality to zero, not n. n doesn't get used at all in the actual calculation.
-
Mar 29th, 2006, 10:08 PM
#3
Thread Starter
Frenzied Member
Re: divide by 0 exception
Yup, you are right.. i Just figured that out.. now I did little bit change, still my Main method does not even work:
Code:
using System;
class division
{
int i;
int j;
public division (int i, int j)
{
this.i = i;
this.j = j;
}
public void divide(int m, int n)
{
try
{
if (n==0) throw new DivideByZeroException("Divide by 0");
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception: {0}", e);
return;
}
Console.WriteLine(m /n);
}
}
class Test
{
public static void Main()
{
division d = new division(3, 1);
Console.WriteLine("Please input two numbers: ");
Console.WriteLine("{0}, {1}", Console.ReadLine(), Console.ReadLine());
d.divide (Console.ReadLine (), Console.ReadLine());
Console.ReadLine ();
}
}
-
Mar 29th, 2006, 10:09 PM
#4
Thread Starter
Frenzied Member
Re: divide by 0 exception
This code is a messy one... if it could be clearly modified, I really appreciate, guys!
-
Mar 29th, 2006, 10:26 PM
#5
Thread Starter
Frenzied Member
Re: divide by 0 exception
This is the updated codes, still not working:
Code:
using System;
class division
{
int i;
int j;
public division (int i, int j)
{
this.i = i;
this.j = j;
}
public void divide()
{
try
{
if (j==0) throw new DivideByZeroException("Divide by 0");
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception: {0}", e);
return;
}
Console.WriteLine(i /j);
}
}
class Test
{
public static void Main()
{
division d = new division(3, 0);
Console.WriteLine("Please input two numbers: ");
Console.WriteLine("{0}, {1}", Console.ReadLine(), Console.ReadLine());
d.divide (Console.ReadLine (), Console.ReadLine());
// d.divide();
Console.ReadLine ();
}
}
-
Mar 29th, 2006, 10:30 PM
#6
Re: divide by 0 exception
What errors are you getting and why are you doing the division outside of the Try and Catch blocks?
-
Mar 29th, 2006, 10:37 PM
#7
Thread Starter
Frenzied Member
Re: divide by 0 exception
Good question. I don't know! Does that division have to be in the try block? That's why I said it is a messy code. Also, I got the error that cannot convert string to Integer
-
Mar 29th, 2006, 10:43 PM
#8
Re: divide by 0 exception
Your 'divide' method is expecting two int objects. Console.ReadLine returns a string so you would have to read the two values, make sure that they are numbers, convert them and then call 'divide'. Also, you're calling ReadLine four times. That means that when the user enters two numbers the first two calls will be used to write the values back to the console, then it will wait for you to enter two more values. You should read from the console twice intot string variables. You can then do whatever you want with those values, like write them to the console or convert them to integers.
-
Mar 29th, 2006, 10:47 PM
#9
Thread Starter
Frenzied Member
Re: divide by 0 exception
why instead of telling me this... cannot you just write the codes for me? So that I could pick it up faster than using the same mind that "created the problem"?
-
Mar 29th, 2006, 10:56 PM
#10
Re: divide by 0 exception
 Originally Posted by vbbit
why instead of telling me this... cannot you just write the codes for me? So that I could pick it up faster than using the same mind that "created the problem"?
If we did everything for you, you wouldn't learn. Besides, this could be a homework assignment and doing so could get you expelled.
Also, there are tons of examples online about receiving console input and making simple calculators and such.
-
Mar 29th, 2006, 10:57 PM
#11
Re: divide by 0 exception
Because it's better if the same mind that created the problem can solve the problem. Writing the code yourself is a better way to understand it than copying and pasting something someone else wrote. Making mistakes is part of the learning process, and so is fixing them.
-
Mar 30th, 2006, 12:20 AM
#12
Thread Starter
Frenzied Member
Re: divide by 0 exception
Thanks guys.. I understand now... I got it worked. YAY!
-
Mar 30th, 2006, 12:22 AM
#13
Re: divide by 0 exception
More satisfying when you do it yourself, isn't it? Don't forget to resolve your thread from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|