Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 - Week 10 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 200 - Week 10 Jim Williams, PhD."— Presentation transcript:

1 CS Week 10 Jim Williams, PhD

2 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

3 Text Interface vs GUI Interface

4 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

5 Which kind of path? jim/pictures absolute relative other
B: relative to current working directory

6 Forward / or Backward \ Windows: C:\Users\Fred In Java program:
Or "C:/Users/Fred" Linux/Unix/Mac /users/fred

7 Command-Line Commands
Windows Unix/Linux/Mac cd mkdir rmdir del type help rm cat man

8 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.

9 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]);

10 Command Line Arguments
Passing to program from within Eclipse Passing to program from Command-Line

11 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

12 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

13 Javadocs 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)

14 Exception Handling Runtime Exceptions Unchecked Exceptions
Error - internal system errors RuntimeException Checked Exceptions Exception

15 ArrayIndexOutOfBounds is
a checked exception an unchecked exception other B: an unchecked exception

16 Checked Exceptions Compiler forces programmer to handle them.
Must be handled, either: catch declare the method throws

17 Throws clause //declare method throws them
public void myMethod() throws IOException { }

18 Catching Exceptions try { some statements; } catch ( Exception1 e) {

19 Throwing Exceptions public void myMethod(int i) throws IllegalArgumentException { if ( i < 0) { throw new IllegalArgumentException( “i cannot be negative”); } //IllegalArgumentException is a RuntimeException

20 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?

21 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;

22 More Exceptions Exception Message
What to do when you have to catch an exception and aren't sure yet how to handle? e.printStackTrace()

23 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

24 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

25 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.

26 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

27 How would you access "Hi."? responses[1][1][1]
Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} }; try it

28 Handling Command Line Arguments


Download ppt "CS 200 - Week 10 Jim Williams, PhD."

Similar presentations


Ads by Google