This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exeption Handling

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Javaranchers,
when is it neccessary to catch an exception of a method?
In the following program I used the read method which throws the IOException according to SUN Java API Specification.
When I compile this error occurs:
C:\jdk1.3\bin>javac read_test1.java
read_test1.java:7: unreported exception java.io.IOException; must be caught or declared to be thrown
int ch = System.in.read ();
^
1 error
 When is it neccessary to catch an exception of a method?
============================================================
public class read_test1
{
public static void main (String args[])
{
while (true)
{
int ch = System.in.read ();
if (ch == 'x')
break;
}
System.out.println("x was read.");
}
}
Thanks for any answer.
Thomas

------------------
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use try and catch in your code when you suspecious that something might go wrong. You can also use throws xxException in your constructor or method, in this case the system will take care of the exception.
You must use try and catch when you executing a statement.
Hope this helps!
Cheers
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All exceptions must either be 1] caught in the method, or 2] declared as part of the methods signiture. In your example since you are already in "main" you will need to catch the exception. Something like this:

 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the read() from the InputStream throws an IOException that exception has to be caught (or thrown) by the method with the call to the read().
Example:

Also you can add throws Exception to the main method:
public static void main( String args[] ) throws Exception
in which case you would not need the try catch for the call to methodC or the call to the read method in your code. The main method would throw the exception to the system.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All exceptions except RuntimeException that are thrown in the method you are invoking must be either caught or thrown in the calling method.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your anwers.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I have another question:
Where do I have to place the try-Block?
I tried it this way:
try {
int ch = System.in.read()
}
I got an error.
I tried it as it was described by christopher:
int ch = 0;
try {
ch = System.in.read();
}
It worked.
Why do I have to keep the declaration of variable "ch" outside
the try-clause?
Thanks for any answers.
Thomas
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, since ch is declared in the try block, its scope is the try block. You won't be able to use ch outside of the block.
I know Christopher's example doesn't use ch outside the try block. If you do, though, it won't work.

[This message has been edited by Roy Tock (edited July 18, 2001).]
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scope is actually pretty important and there are quite a few subtle aspects to it and you should get a good Java book and read up on it. But basically, in the case you presented, the { and } delienate the beginning and ending of a block of code and the variable is only accessable within that block. The reason why the second case works is that the code block where the variable is used is inside the code block where the variable is created and therefore accessable
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scope! Is very important.
Here is some more info about try & catch: http://java.sun.com/docs/books/tutorial/essential/exceptions/try.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic