Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws DivideByZeroException ?

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have constructed a simple program to test my knowledge on Exceptions using the throws statement.The main method contains the try and catch blocks ,where I have used a DivideByZeroException statement.It does not compile , complaing that it does not recognise this method.I have tried Exception & ArimeticException and both work.I have looked trough the API and cannot find DivideByZeroException even though my text book displays this.Can anyone tell me if this exists or am I being mislead.I have added the code below just in case I am missing something
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good for you for exploring Java by writing code!

There's no DivideByZeroException. You probably want ArithmeticException, which is in java.lang. It only gets thrown when an integer is divided by integer zero.

Ugh, so does every integer division have to appear in a try block? No, fortunately, because ArithmeticException is a runtime exception. That means you don't need to catch it, and in fact you shouldn't. Runtime exceptions indicate conditions that are completely avoidable, so they should be dealt with by rewriting the code. In your example, if you were writing production code rather than experimental code, the presence of ArithmeticException would indicate that your code divided something by zero. Since division by zero is meaningless, the exception would indicate that part of your algorithm didn't mean anything! It's best not to ship code that doesn't mean anything.

There's an animated illustration in Chapter 11 of "Ground-Up Java" that lets you design a snippet of exception-throwing code, and then animates the exception throwing and catching mechanism.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by daniel hyslop:
...I have looked trough the API and cannot find DivideByZeroException even though my text book displays this. Can anyone tell me if this exists or am I being mislead....

I do not believe there is a DivideByZeroException in the current API. However, you can define your own exceptions, and I suspect this is what your textbook is doing.

Once you have your exception defined (as a class), then you'll need to add code to the method that throws a new instance of that exception if appropriate.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future reference, you should post the EXACT ERROR MESSAGE. Paraphrasing typically loses a lot of useful information that we need in order to help you.

I suspect that the DivideByZero error that you are trying to use is a custom exception that is listed in the book's example. Take a closer look and if you still need help see my first comments ;-)

Good Luck and Keep Coding!

Layne
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my text book there are several examples of multiple catch blocks and it uses the DivideByZeroException as an example of a sub class of Exception but does not state it as user defined.It appears in the chapter about exceptions before we are taught about user defined Exceptions,which was probably misleading me, thanks for the help all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic