CS 200 - Week 10 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Problems with error reporting so far –Either ignored exceptions or terminated program on first error. –Error handling and regular code mixed.
Exceptions Problems with error reporting so far –Either ignored exceptions or terminated program on first error. –Error handling and regular code mixed.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Java Exceptions a quick review….
Exceptions In this lecture:
Exceptions: When things go wrong
Recitation 2 Exception handling.
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Introduction to OO Program Design
CS102 – Exceptions David Davenport Latest: May 2015
Exceptions 10-Nov-18.
CS 302 Week 15 Jim Williams, PhD.
Exceptions 10-Nov-18.
CS Week 11 Jim Williams, PhD.
CS 200 File Input and Output
Handling Exceptions.
Advanced Programming Behnam Hatami Fall 2017.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
CS 200 Command-Line Arguments & Exceptions
Exceptions & exception handling
ATS Application Programming: Java Programming
Exceptions & exception handling
Chapter 12 Exception Handling
File I/O ICS 111: Introduction to Computer Science I
Lecture 9: Exceptions in Java CS201j: Engineering Software
Exception Handling in Java
CS 200 Command-Line Arguments & Exceptions
Web Design & Development Lecture 7
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
CS 302 Week 13 Jim Williams, PhD.
Lecture 11 Objectives Learn what an exception is.
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Tutorial MutliThreading.
Exceptions 10-May-19.
CS 200 Command-Line Arguments & Exceptions
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exceptions 5-Jul-19.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

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