wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Exeption Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exeption Handling" Watch "Exeption Handling" New topic
Author

Exeption Handling

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Jan 12, 2001
Posts: 121
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

Joined: Mar 16, 2001
Posts: 984

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:


Please ignore post, I have no idea what I am talking about.
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
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

Joined: May 05, 2000
Posts: 13974
All exceptions except RuntimeException that are thrown in the method you are invoking must be either caught or thrown in the calling method.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Thanks for your anwers.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Jul 16, 2001
Posts: 83
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

Joined: Oct 27, 2000
Posts: 61
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

Joined: Mar 16, 2001
Posts: 984

Scope! Is very important.
Here is some more info about try & catch: http://java.sun.com/docs/books/tutorial/essential/exceptions/try.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exeption Handling
 
Similar Threads
execution of main and static initialiser
Checked Exceptions - What did I wrong?
System.in.read() - Abstract method?
why wont this run
int and char