Created
July 15, 2016 22:09
-
-
Save pyq/a56eb3ed6529b413c0790f2872922991 to your computer and use it in GitHub Desktop.
See what error you throw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.EOFException; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.IllegalFormatException; | |
/** | |
* Created by yongqingpeng on 7/15/16. | |
*/ | |
class SuperClass | |
{ | |
void methodA() | |
{ | |
System.out.println("super methodA"); | |
} | |
void methodWithSameException() throws FileNotFoundException | |
{ | |
throw new FileNotFoundException("super methodWithSameException"); | |
} | |
void methodHasExceptionInSuper() throws FileNotFoundException | |
{ | |
throw new FileNotFoundException("super methodWithSameException"); | |
} | |
void methodWithDiffException() throws EOFException | |
{ | |
throw new EOFException("super methodWithSameException"); | |
} | |
} | |
public class SubClass extends SuperClass | |
{ | |
void methodA() | |
{ | |
System.out.println("sub methodA"); | |
} | |
void methodB() | |
{ | |
System.out.println("sub methodB"); | |
} | |
void methodWithSameException() throws FileNotFoundException | |
{ | |
throw new FileNotFoundException ("sub methodWithSameException"); | |
} | |
void methodHasExceptionInSuper() | |
{ | |
System.out.println("sub methodHasExceptionInSuper"); | |
} | |
// can't throw FileNotFoundException (different type) | |
//void methodWithDiffException() throws FileNotFoundException | |
// can't throw superType IOException (EOFException extends IOException) | |
//void methodWithDiffException() throws IOException | |
// can throw SubEOFException | |
void methodWithDiffException() throws SubEOFException | |
{ | |
throw new SubEOFException("sub methodHasExceptionInSuper"); | |
} | |
public static void main(String[] args) | |
{ | |
SuperClass superTypeSubInstance = new SubClass(); | |
// I can call methodA, sub methodA | |
superTypeSubInstance.methodA(); | |
// I can't call methodB because no methodB in SuperClass | |
// superTypeSubInstance.methodB(); | |
// conclusion: | |
// which method you can call based on referring type (SuperClass) | |
// which method you will run based on actually instance type (SubClass) | |
// for the same exception, compile could make sure we handle "FileNotFoundException" | |
try | |
{ | |
superTypeSubInstance.methodWithSameException(); | |
} | |
catch (FileNotFoundException e) | |
{ | |
// print java.io.FileNotFoundException: sub methodWithSameException | |
System.out.println("catch error: " + e); | |
} | |
// for case only super class throws exception, we still need to handle this exception | |
// because compiler only know superTypeSubInstance is super type. | |
try | |
{ | |
superTypeSubInstance.methodHasExceptionInSuper(); | |
} | |
catch (FileNotFoundException e) | |
{ | |
// won't reach here, no error throw in sub | |
System.out.println("catch error: " + e); | |
} | |
// case for both classes have exceptions | |
try | |
{ | |
superTypeSubInstance.methodWithDiffException(); | |
} | |
catch (EOFException e) | |
{ | |
// if we catch EOFException | |
// we could still catch SubEOFException | |
// print "catch error: SubEOFException: sub methodHasExceptionInSuper" | |
System.out.println("catch error: " + e); | |
} | |
// conclusion | |
// subclass overridden method can declare same, subclass exception or no exception | |
// this guarantee compiler always handle all checked exception no matter what the actually object type is | |
} | |
} | |
class SubEOFException extends EOFException | |
{ | |
public SubEOFException(String s) | |
{ | |
super(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment