Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tutorial 35 (Multiple Exceptions)
- //App.java
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.text.ParseException;
- public class App {
- public static void main(String[] args) {
- Test test = new Test();
- /*
- // Multiple catch blocks
- try {
- test.run();
- } catch (IOException e) {
- } catch (ParseException e) {
- System.out.println("Couldn't parse command file.");
- }
- */
- /*
- //Try Multi-catch (Java 7+ only)
- try {
- test.run();
- } catch (IOException | ParseException e) { //this is the Try Multi Catch. It can handle multiple exceptions
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- */
- //Using polymorphisim to catch the parent of all exceptions (ie: IOException, ParseException or any child classes of exception)
- try {
- test.run();
- } catch (Exception e) { //all exceptions derive from the parent class exception: "Exception" //thus just like we're passing methods to a parameter, we could catch any exception by using a parent class here "catch (parent-class-Exception e)"
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Important to catch exceptions in the right order!
- // IOException cannot come first, because it's the parent
- // of FileNotFoundException, so would catch both exceptions
- // in this case.
- try {
- test.input();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------------
- //Test.java
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.text.ParseException;
- public class Test {
- public void run() throws IOException, ParseException {
- //throw new IOException();
- throw new ParseException("Error in command list.", 2);
- }
- public void input() throws IOException, FileNotFoundException {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement