Created
August 19, 2021 23:21
-
-
Save CemraJC/7a8c06e4bbaa26801e6914ccc032fea1 to your computer and use it in GitHub Desktop.
Prac4_Wk4_Fri8
This file contains 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.File; | |
import java.io.FileNotFoundException; | |
import java.util.Random; | |
public class PracticalFour { | |
/** | |
* Throw an exception of the given 'type' in the arguments list, | |
* or print "No problems" if 'NONE' | |
* | |
* Work on this until: 8:21 | |
* | |
* @param type The type of error to throw (or NONE) | |
*/ | |
private static void makeException(ExceptionEnum type) | |
throws FileNotFoundException, UnknownException { | |
switch (type) { | |
case NONE: | |
System.out.println("No problems"); | |
break; | |
case NULL: | |
throw new NullPointerException(); | |
case BOUNDS: | |
throw new ArrayIndexOutOfBoundsException(); | |
case MISSING: | |
throw new FileNotFoundException(); | |
case UNKNOWN: | |
throw (new UnknownException()); | |
/* throw <object> */ | |
} | |
} | |
private static void f() { | |
try { | |
try { | |
makeException(ExceptionEnum.NULL); | |
} catch (NullPointerException e) { | |
System.out.println("Null was squashed"); | |
// Squash | |
} | |
makeException(ExceptionEnum.NONE); | |
makeException(ExceptionEnum.MISSING); | |
makeException(ExceptionEnum.NONE); | |
} catch (Exception e) { // BAD! | |
System.out.println(e.toString()); | |
} | |
} | |
// Work on h() until 9:02 | |
private static void h(Random random) throws UnknownException { | |
try { | |
g(random); | |
} catch (FileNotFoundException e) { | |
System.out.println(e); | |
} finally { | |
System.out.println("Reached here!"); | |
} | |
} | |
private static void g(Random random) throws FileNotFoundException, UnknownException { | |
int x = random.nextInt(ExceptionEnum.values().length); | |
int y = random.nextInt(ExceptionEnum.values().length); | |
try { | |
makeException(ExceptionEnum.values()[x]); | |
System.out.println("x = " + x); | |
makeException(ExceptionEnum.values()[y]); | |
System.out.println("y = " + y); | |
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) { | |
System.out.println(e); | |
} | |
} | |
public static void main(String[] args) { | |
f(); | |
Random random = new Random(); | |
for (int i = 0; i < 5; i++) { | |
try { | |
h(random); | |
} catch (UnknownException e) { | |
System.out.println(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment