CS 200 - Week 10 Jim Williams, PhD
This Week MineSweeper, Milestone 3 Team Lab: ArrayLists First impressions matter! Comment and style as you go Having difficulty reading partner's code? Formatting printMap Team Lab: ArrayLists Lecture: Command-line arguments, paths and Exceptions
Text Interface vs GUI Interface
Current Working Directory A specific path in the file system Relative paths: relative to current working directory images/file.jpg Absolute paths: specified from root directory C:\Users\jimw\workspace\images /Users/jimw
Which kind of path? jim/pictures absolute relative other B: relative to current working directory
Forward / or Backward \ Windows: C:\Users\Fred In Java program: Or "C:/Users/Fred" Linux/Unix/Mac /users/fred
Command-Line Commands Windows Unix/Linux/Mac cd mkdir rmdir del type help rm cat man
What are command-line arguments? an array of Strings information passed into the program the main method parameter public class CmdLine { public static void main(String[] args) { System.out.println( args.length); for ( int i = 0; i < args.length; i++) { System.out.println( args[i]); } All are true The main method parameter is named args that is an array of Strings that contains information passed into the program.
Usage Message public class CmdLine { public static void main(String[] args) { if ( args.length != 1) { System.out.println( "Usage: java CmdLine filename"); System.exit( 1); //0 success, non-zero error } System.out.println( "Filename: " + args[0]);
Command Line Arguments Passing to program from within Eclipse Passing to program from Command-Line
3 Categories of Throwables Checked Exceptions Require exception handling code Unchecked Exceptions Programmer error, in general fix, don't wrap with handling code Errors System configuration and other VM problems
Unchecked Exceptions - Programming Errors System.out.println( 1 / 0); //ArithmeticException int[] list = new int[4]; System.out.println( list[4]); //ArrayIndexOutOfBoundsException String s = “abc”; System.out.println( s.charAt(3)); //StringIndexOutOfBoundsException Object o = new Object(); String d = (String)o; //ClassCastException Object o = null; o.toString(); //NullPointerException
Javadocs http://docs.oracle.com/javase/8/docs/api/java/lang/RuntimeException.html Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String at Test.method2(Test.java:5) at Test.method1(Test.java:9) at Test.main(Test.java:27)
Exception Handling Runtime Exceptions Unchecked Exceptions Error - internal system errors RuntimeException Checked Exceptions Exception http://www.javamex.com/tutorials/exceptions/exceptions_hierarchy.shtml
ArrayIndexOutOfBounds is a checked exception an unchecked exception other B: an unchecked exception
Checked Exceptions Compiler forces programmer to handle them. Must be handled, either: catch declare the method throws
Throws clause //declare method throws them public void myMethod() throws IOException { }
Catching Exceptions try { some statements; } catch ( Exception1 e) {
Throwing Exceptions public void myMethod(int i) throws IllegalArgumentException { if ( i < 0) { throw new IllegalArgumentException( “i cannot be negative”); } //IllegalArgumentException is a RuntimeException
What happens when... try { statement1; statement2; //throws an exception statement3; } catch( Exception1 e) { } catch( Exception2 e) { } statement4; //will statement 3 be executed? //if exception is not caught will statement 4 be executed? //if the exception is caught will statement 4 be executed?
What happens when... int methodA() throws Exception5 { try { statement1; statement2; statement3; } catch( Exception1 e) { statementE; throw new Exception5(“some message”); } finally { statement4; } Statement5; return 1;
More Exceptions Exception Message What to do when you have to catch an exception and aren't sure yet how to handle? e.printStackTrace()
If methodA doesn't throw an exception then output is? public static void main(String[] args) throws MyException { try { methodA( args); System.out.print("B"); } catch( MyException e) { throw e; System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); BCDE CDE CD BDE try it
If methodA throws MyException then output is? public static void main(String[] args) throws MyException { try { methodA( args); System.out.print("B"); } catch( MyException e) { //e.printStackTrace(); System.out.print("C"); throw e; } finally { System.out.print("D"); } System.out.print("E"); BCDE CDE CD BDE try it
If this compiles, what is true? static void methodB(int i) { if ( i < 3) throw new ExceptionQ(); } public static void main(String[] args) { try { methodB( 3); System.out.print("B"); } catch( ExceptionQ e) { System.out.print("C"); } finally { System.out.print("D"); System.out.print("E"); ExceptionQ is an checked exception CDE is printed D only is printed BDE is printed ExceptionQ is an unchecked exception. D: BDE is printed.
If methodA throws MyException2 then output is? public static void main(String[] args) throws MyException { try { methodA( 3); System.out.print("B"); } catch( MyException2 e) { System.out.print("C"); } catch( MyException e) { throw e; System.out.print("D"); } System.out.print("E"); BCE CDE CE BD try it
How would you access "Hi."? responses[1][1][1] Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} }; try it
Handling Command Line Arguments