R
Razvan
Hi !
I am just trying to understand how exceptions must be specified on
overriden functions. Take a look at the following code:
import java.io.IOException;
class CBase
{
public static int testX() throws RuntimeException { return 1;}
public static int testX2() throws IOException { return 1;}
}
public class CDummy extends CBase
{
public static void main(String args[])
{
}
public static int testX() throws IndexOutOfBoundsException { return
2;}
//public static int testX2() throws Exception { return 1;}
}
I understand that the overriden functions can throw only exceptions
that are derived from the exception thrown by the function from the
superclass. Can somebody explain why we have this restriction ? Does
it matter if the exception is checked or not ?
How about the reverse ? If the function from the superclass does not
throw anything, what can be said about the functions that override it
?
Regards,
Razvan
I am just trying to understand how exceptions must be specified on
overriden functions. Take a look at the following code:
import java.io.IOException;
class CBase
{
public static int testX() throws RuntimeException { return 1;}
public static int testX2() throws IOException { return 1;}
}
public class CDummy extends CBase
{
public static void main(String args[])
{
}
public static int testX() throws IndexOutOfBoundsException { return
2;}
//public static int testX2() throws Exception { return 1;}
}
I understand that the overriden functions can throw only exceptions
that are derived from the exception thrown by the function from the
superclass. Can somebody explain why we have this restriction ? Does
it matter if the exception is checked or not ?
How about the reverse ? If the function from the superclass does not
throw anything, what can be said about the functions that override it
?
Regards,
Razvan