M
Matt
Method evenNumber can throw IOException, and the caller of method
evenNumber (in this case, method evenNumberTest and method evenNumberTest2)
should catch the IOException. Otherwise, it will have compile
error as the one in method evenNumberTest2.
My question is when I run the program, it will have output "catch evenNumberTest",
but it doesn't output "throw testNumber" Why is that?
I thought ""throw new IOException("throw testNumber");"" will print
"throw testNumber" when it throws the IOException.
So in what situation "throw testNumber" will print it out??
Please advise. Thanks!!
==========================================================
import java.io.*;
public class ExceptionTest
{
private void evenNumberTest()
{ try
{ evenNumber(2);
}
catch(IOException e)
{ System.out.println("catch evenNumberTest");
}
}
/**
ExceptionTest.java:14: unreported exception java.io.IOException; must be caught
or declared to be thrown
{ evenNumber(2);
^
private void evenNumberTest2()
{ evenNumber(2);
}
*/
private void evenNumber(int x) throws IOException {
if (x % 2 == 0)
throw new IOException("throw testNumber");
}
public static void main(String[] args)
{ ExceptionTest e = new ExceptionTest();
e.evenNumberTest();
}
}
evenNumber (in this case, method evenNumberTest and method evenNumberTest2)
should catch the IOException. Otherwise, it will have compile
error as the one in method evenNumberTest2.
My question is when I run the program, it will have output "catch evenNumberTest",
but it doesn't output "throw testNumber" Why is that?
I thought ""throw new IOException("throw testNumber");"" will print
"throw testNumber" when it throws the IOException.
So in what situation "throw testNumber" will print it out??
Please advise. Thanks!!
==========================================================
import java.io.*;
public class ExceptionTest
{
private void evenNumberTest()
{ try
{ evenNumber(2);
}
catch(IOException e)
{ System.out.println("catch evenNumberTest");
}
}
/**
ExceptionTest.java:14: unreported exception java.io.IOException; must be caught
or declared to be thrown
{ evenNumber(2);
^
private void evenNumberTest2()
{ evenNumber(2);
}
*/
private void evenNumber(int x) throws IOException {
if (x % 2 == 0)
throw new IOException("throw testNumber");
}
public static void main(String[] args)
{ ExceptionTest e = new ExceptionTest();
e.evenNumberTest();
}
}