Click to See Complete Forum and Search --> : Exception Handling


ashwani singhal
September 29th, 1999, 06:12 AM
I have written following code. When i compile it , it gives the error message

import java.lang.*;
import java.io.*;
import java.util.*;

public class Short
{
public static void main(String args[])
{
System.in.read();
}

}

Error message

Excpetion java.io.IOException must be caught

Erbeere
September 29th, 1999, 06:15 AM
You must write the main like that:

public static void main(String args[])
{
try
{
System.in.read();
}
catch (IOException e)
{
}
}

ashwani singhal
September 29th, 1999, 06:28 AM
Thanks Erbeere

I have a question for you
If any function declares as it throws exception
and when we use it in any code block ,
is it necessary that code block should be in
try {}

Erbeere
September 29th, 1999, 06:32 AM
Yes
If a method throws an exception than you must set it in a try block and directly after that a catch block.
If a method throws more exceptions than one, you only set one try but more catch blocks.
For the beginning and if you speak German than it is the easiest if you will search for the Book "Java in 21 Tagen" there is everything explained really good but I donŽt know if youŽll find this in English or another language