Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions clarification

 
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above is one of the incorrect answers to a question on Exceptions. The explanation given is that in order to use the above, java.io should be imported or the exception should be declared with a fully qualified name. What is the fully qualifed name of this exception?

The entire program is as follows:

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shuba,

IOException is located in java.io, so the fully qualified name would be java.io.IOException (and the program will compile successfully if you make that change). That is not the only thing that is fishy with this code, though.

By the way, if you're not sure where something like this is located, you can go to the Java API docs, click on "All Classes" in the top left frame, and then scroll down the list to find the item you're looking for.
 
shuba gopal
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kurt, thanks. I now see what that line means. The 7/x throws runtime exception. All the answer options are about declaring this exception.
 
Greenhorn
Posts: 13
Android Python C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are tons of ways to work with Exceptions, and Exception types.
My favorite (And probably not the best method) is using try/catch
Example:


Generally i try to use just the simple java Exception, because it will give information about ANY Exception thrown about what you are trying to do. Further down the line your programs may need to do certain things based upon what error is thrown, that is where you would want to become more specific in what Exception you want to be looking for.

This will print to your console what exception is being thrown, from there you can do a few quick google searches to find out what the problem may be. But in this case its because you are trying to divide by zero.

if you try to run that, it will print out:

java.lang.ArithmeticException: / by zero


and a java.lang.ArithmeticException: / by zero search will give you more information on why that is bad.
 
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a lot in the Java™ Tutorials about Exceptions.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic