• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions clarification

 
shuba gopal
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • 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:

 
Kurt Van Etten
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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: 76
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
David Freitag
Greenhorn
Posts: 13
Android C++ Python
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot in the Java™ Tutorials about Exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic