• 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

Exeption Handling

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

------------------
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your anwers.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic