Download presentation
Presentation is loading. Please wait.
1
CS 302 Week 13 Jim Williams, PhD
2
This Week Team Lab: Processing Optional Labs More Exceptions
File Output and Input
3
More Exceptions Exception Message
What to do when you have to catch an exception and aren't sure yet how to handle? e.printStackTrace()
4
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
5
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
6
What is the correct output?
Hello Fred 100,3.14 Hello Fred 100, 00100,3.14 public static void main(String[] args) { String name = "Fred"; int num = 100; double dNum = ; System.out.printf("Hello %6s\n", name); System.out.printf("%05d,%.2f", num, dNum); }
7
Files
8
What does this do? String name = "myfile.txt";
File aFile = new File(name); PrintWriter output = new PrintWriter( aFile); output.printf("This is the contents of %s.\n", name); output.close();
9
What does this do? PrintWriter writer = null; try {
writer = new PrintWriter( SEEDS_FILENAME); for ( Seed s : seeds) { writer.printf("%f, %f\n", s.getX(), s.getY()); } } catch ( IOException e) { System.err.println("Unable to write to " + SEEDS_FILENAME); } finally { if ( writer != null) writer.close();
10
What does this do? File file = new File(SEEDS_FILENAME);
Scanner inFile = null; try { inFile = new Scanner(file); while (inFile.hasNext()) { //process each line of the file } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (inFile != null) inFile.close();
11
Relative vs Absolute Paths
Relative - based on current location Absolute - based on fixed location
12
From within log what is a relative path to tasks?
/ meta log mylyn taskListIndex segments tasks contexts plugins history ../mylyn/tasks /meta/mylyn/tasks ./mylyn/tasks ../tasks
13
From within tasks what is a relative path to mylyn?
/ meta log mylyn taskListIndex segments tasks contexts plugins history /meta/mylyn ../mylyn ./../mylyn/tasks ..
14
From within tasks what is an absolute path to log?
/ meta log mylyn taskListIndex segments tasks contexts plugins history /meta/log ../../log ./meta/log /log
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.