• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

throws DivideByZeroException ?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I am Arthur, King of the Britons. And this is a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic