When would you use the try block and the catch block? Thank you in advance.
try
{
IoObj.Read()
}
catch (IOException e)
{
// catch exception
}
String s = stdin.readLine();
int i;
try {
i = Integer.parseInt(s);
} catch(NumberFormatException e){
//insert what you want to do here
}
That's not true. When calling a function that explicitly throwsOriginally posted by pyu:
he other kind I can think of --> nameofmethod(int i) throws typeOfException -- doesn't do that. The compiler catches that error and stops the program.
TypeOfException
try {...}
catch (TypeOfException myException) {...}
Also, the java compiler will force you catch exceptions
int x = wham();
int x;
try
{
x = wham();
}
catch(Exception e)
{
//do something with e
}
int x = 0;
try
{
x = wham();
}
catch(Exception e)
{
//do something with e
}