Algorithmics 3 Algorithmics 3 Error Management October 18th, 2006 Algorithmics 3 Program Program Errors About errors Exceptions Java choices A first look at I/O Bottom line I/O About I/O Algorithmics 3 Program Warning Some of you can’t program yet. Algorithmics 3 Program Warning Some of you can’t program yet. It’s unfortunate, but it’s not too late. Algorithmics 3 Program Warning Some of you can’t program yet. It’s unfortunate, but it’s not too late. So, let me give a little advice. Algorithmics 3 Program Programming is not Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I Writing random things found in the lecture. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I Writing random things found in the lecture. Random things found on the Internet. I Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I I Writing random things found in the lecture. Random things found on the Internet. I Giving random answers. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I I Writing random things found in the lecture. Random things found on the Internet. I Giving random answers. I Adding stuff until it works. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I I Writing random things found in the lecture. Random things found on the Internet. I Giving random answers. I Adding stuff until it works. Hoping that the answer to every single question appears in the lecture. I Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I I Writing random things found in the lecture. Random things found on the Internet. I Giving random answers. I I Adding stuff until it works. Hoping that the answer to every single question appears in the lecture. I Waiting for the end of the class. Algorithmics 3 Program Programming is not I Nodding approvingly whenever you see some source code. I Looking at a blank screen waiting for the miracle to come. I I Writing random things found in the lecture. Random things found on the Internet. I Giving random answers. I I Adding stuff until it works. Hoping that the answer to every single question appears in the lecture. I Waiting for the end of the class. Note that most of these remarks are valid for all sciences. Algorithmics 3 Program Programming is Algorithmics 3 Program Programming is I Reasoning. Algorithmics 3 Program Programming is I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. Algorithmics 3 Program Programming is I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Algorithmics 3 Program Programming is I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Algorithmics 3 Program Programming is I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Algorithmics 3 Program Programming is I I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Finding simple solutions to simple problems. Algorithmics 3 Program Programming is I I I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Finding simple solutions to simple problems. Understanding the solution well enough to be able to explain them without the slightest ambiguity. Algorithmics 3 Program Programming is I I I I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Finding simple solutions to simple problems. Understanding the solution well enough to be able to explain them without the slightest ambiguity. Having a clear idea of what the tools will let you do and what they won’t. Algorithmics 3 Program Programming is I I I I I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Finding simple solutions to simple problems. Understanding the solution well enough to be able to explain them without the slightest ambiguity. Having a clear idea of what the tools will let you do and what they won’t. Understanding what you read (typically, in a documentation). Algorithmics 3 Program Programming is I I I I I I I I Reasoning. Explaining things clearly in a foreign language to someone deaf, blind, stupid and who remembers every single of your contradictions. (The computer, mind you, not the teacher). Checking that the deaf, blind, stupid person understands what you have explained. Understanding problems well enough to be able to explain them without the slightest ambiguity. Finding simple solutions to simple problems. Understanding the solution well enough to be able to explain them without the slightest ambiguity. Having a clear idea of what the tools will let you do and what they won’t. Understanding what you read (typically, in a documentation). Once again, it’s not that different from what you’re asked in other sciences and technologies. Algorithmics 3 Program So ? If you can succeed in maths or physics, you can succeed in programming. Algorithmics 3 Program Exam For the exam, you’ll be allowed to use just about everything, including the lectures, google, Javasoft’s website. . . Algorithmics 3 Program Exam For the exam, you’ll be allowed to use just about everything, including the lectures, google, Javasoft’s website. . . Well, ok, not everything: not cellphones, your e-mail or Messenger, for instance. Algorithmics 3 Program Exam For the exam, you’ll be allowed to use just about everything, including the lectures, google, Javasoft’s website. . . Well, ok, not everything: not cellphones, your e-mail or Messenger, for instance. None of this will help you if you can’t program. Algorithmics 3 Program Time for questions Is there any question ? Algorithmics 3 Program Time for questions Is there any question ? If there’s no question, I’ll assume that you have understood everything up to this point, including Algorithmics 1 and Algorithmics 2. Algorithmics 3 Program Time for questions Is there any question ? If there’s no question, I’ll assume that you have understood everything up to this point, including Algorithmics 1 and Algorithmics 2. In particular, in lab classes, I may decide that I have spent enough time trying to help you at the whiteboard. Algorithmics 3 Program Time for questions Is there any question ? If there’s no question, I’ll assume that you have understood everything up to this point, including Algorithmics 1 and Algorithmics 2. In particular, in lab classes, I may decide that I have spent enough time trying to help you at the whiteboard. So ? Algorithmics 3 Program Today’s program 1. Errors. 2. Exceptions. 3. A few words about files. Algorithmics 3 Program Note The first part of this session is back to algorithmics. We’re going to introduce a new algorithmic construction, available in most modern programming languages: exceptions. Algorithmics 3 Program Note The first part of this session is back to algorithmics. We’re going to introduce a new algorithmic construction, available in most modern programming languages: exceptions. The second part of this session is, as Swing, putting algorithmics and object-orientation to work. Algorithmics 3 Errors About errors Program Errors About errors Exceptions Java choices A first look at I/O Bottom line I/O About I/O Algorithmics 3 Errors About errors Errors When can errors happen ? Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. You should fix this. Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. You should fix this. type error The computer guesses you’re doing something counter-natural. Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. You should fix this. type error The computer guesses you’re doing something counter-natural. You should fix this. Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. You should fix this. type error The computer guesses you’re doing something counter-natural. You should fix this. runtime error Something wrong happens. Algorithmics 3 Errors About errors Errors When can errors happen ? syntax error The computer doesn’t understand your program. You should fix this. type error The computer guesses you’re doing something counter-natural. You should fix this. runtime error Something wrong happens. Your program should handle this. Algorithmics 3 Errors About errors Runtime errors Let’s take a look at division function d i v i d e (p : integer , q : integer ) : double begin d i v i d e := p/q end Q What’s missing ? Algorithmics 3 Errors About errors Runtime errors Let’s take a look at division function d i v i d e (p : integer , q : integer ) : double begin d i v i d e := p/q end Q What’s missing ? A Well, error management ? Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Why ? Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Why ? I It’s not always possible to return a special value. Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Why ? I It’s not always possible to return a special value. Why ? Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Why ? I It’s not always possible to return a special value. Why ? I gotos are evil. Algorithmics 3 Errors About errors Ideal error management Q Ideally, what should happen ? Q What are the difficulties ? I You need to be able to stop a computation or an operation when something goes wrong. I Abruptly stopping the program is too brutal. Why ? I It’s not always possible to return a special value. Why ? I gotos are evil. I The library designer typically doesn’t know how errors/exceptional circumstances should be handled. Algorithmics 3 Errors Exceptions Program Errors About errors Exceptions Java choices A first look at I/O Bottom line I/O About I/O Algorithmics 3 Errors Exceptions Introducing exceptions Exceptions are the standard manner of dealing with exceptional circumstances, that require stopping a computation. Algorithmics 3 Errors Exceptions Introducing exceptions Exceptions are the standard manner of dealing with exceptional circumstances, that require stopping a computation. Typical exceptional circumstances: I disk error (the disk is full, the file doesn’t exist. . . ) I the user has given you an inacceptable value I this part of the program hasn’t been written yet ... I Algorithmics 3 Errors Exceptions How exceptions work 1. Some algorithm A is declared as being able to trigger a set of exceptional circumstances. Algorithmics 3 Errors Exceptions How exceptions work 1. Some algorithm A is declared as being able to trigger a set of exceptional circumstances. 2. Any algorithm using A must declare that it is going to try and execute A – and how it is going to handle the exceptional circumstance. Algorithmics 3 Errors Exceptions How exceptions work 1. Some algorithm A is declared as being able to trigger a set of exceptional circumstances. 2. Any algorithm using A must declare that it is going to try and execute A – and how it is going to handle the exceptional circumstance. 3. If, for some reason, A triggers some exceptional circumstance, the execution of the program immediately jumps to the exception handler and proceeds from there. 4. Otherwise, the algorithm proceeds normally. Algorithmics 3 Errors Exceptions Syntax and semantics – callee public { double division ( double p , double q) throws MyArithmeticExceptionException i f ( q==0) { t h r o w new M y A r i t h m e t i c E x c e p t i o n E x c e p t i o n ( ) ; // The f u n c t i o n e x i t s , no v a l u e i s r e t u r n e d . } return p/q ; } I Exceptions must be declared. Exceptions may be thrown. I When the exception is thrown, the execution of the method ends. I Algorithmics 3 Errors Exceptions Syntax and semantics – caller public { double try } actionPerformed ( ActionEvent e) { catch double result = d i v i s i o n (p , q ) ; S t r i n g m e s s a g e = " The ␣ r e s u l t ␣ o f ␣ t h e ␣ d i v i s i o n ␣ "+p+" / "+q+" ␣ i s ␣ "+ r e s u l t ; JOptionPane . showMessage ( n u l l , message ) ; ( MyArithmeticExceptionException exc ) { JOptionPane . showMessage ( n u l l , " E r r o r : ␣ d i v i s i o n ␣ by ␣ z e r o " , J O p t i o n P a n e . ERROR_MESSAGE ) ; } } I Exceptions must be either caught or re-declared. I The caller must declare that it is going to try executing some block. The caller must declare what it is going to do if it catches an exception. I Algorithmics 3 Errors Exceptions Benefits of exceptions Q Any idea why exceptions are good ? Algorithmics 3 Errors Exceptions Benefits of exceptions Q Any idea why exceptions are good ? A Here are a few: 1. Normal code is separated from error-handling. Your code is not cluttered with error-checking. 2. There is a clear notion of what a method knows how to handle and what it doesn’t. 3. You can get detailed error reports. 4. You can handle different errors differently. Algorithmics 3 Errors Exceptions Exception design try { // // Do e r r o r −p r o n e o p e r a t i o n s and e v e r y t h i n g t h a t // } catch ( S o m e E x c e p t i o n e1 ) { // // Ha ndle t h e e r r o r // } catch ( A n o t h e r E x c e p t i o n e2 ) { // Ha ndle t h e e r r o r } finally { // // P e r f o r m c l e a n −up // } Algorithmics 3 Errors Exceptions Finally ? Oh, yeah, finally. Algorithmics 3 Errors Exceptions Finally ? Oh, yeah, finally. try { // . . . a t t e m p t do s o m e t h i n g } finally { // . . . t h i s w i l l a l w a y s be e x e c u t e d } Algorithmics 3 Errors Exceptions Finally ? Oh, yeah, finally. try { // . . . a t t e m p t do s o m e t h i n g } finally { // . . . t h i s w i l l a l w a y s be e x e c u t e d } Keyword finally lets us define a block that will always be executed, no matter what happens in the try block or in any catch block. Algorithmics 3 Errors Exceptions What’s the point ? What’s the point of finally ? Algorithmics 3 Errors Exceptions What’s the point ? What’s the point of finally ? // L e t ’ s try { } } play a short animation // Open t h e window // Load i m a g e 1 −− t h i s c a n f a i l // Load i m a g e 2 −− t h i s c a n f a i l // . . . // I f l o a d i n g was t o o l o n g , s t o p now w h i l e ( /∗ l i t t l e t i m e h a s e l a p s e d ∗/ ) { // D i s p l a y n e x t i m a g e −− t h i s } // . . . finally { //No m a t t e r what h a s h a p p e n e d // C l o s e t h e window can fail Algorithmics 3 Errors Exceptions More generally try { // // Do e r r o r −p r o n e o p e r a t i o n s and e v e r y t h i n g t h a t // } catch ( S o m e E x c e p t i o n e1 ) { // // Ha ndle t h e e r r o r // } catch ( A n o t h e r E x c e p t i o n e2 ) { // Ha ndle t h e e r r o r } finally { // // P e r f o r m c l e a n −up // } Algorithmics 3 Errors Exceptions Throwing exception Of course, you can also throw an exception by yourself. Algorithmics 3 Errors Exceptions Throwing exception Of course, you can also throw an exception by yourself. throw new Exception("I have a problem"); throw new URISyntaxExpression("http;//www.univ-orleans.fr", "Your URI doesn’t mean anything."); Algorithmics 3 Errors Exceptions Throwing exception Of course, you can also throw an exception by yourself. throw new Exception("I have a problem"); throw new URISyntaxExpression("http;//www.univ-orleans.fr", "Your URI doesn’t mean anything."); . . . or re-throw it ! try { // . . . } catch ( U R I S y n t a x E x c e p t i o n e ) { // . . . throw e ; } Algorithmics 3 Errors Java choices Program Errors About errors Exceptions Java choices A first look at I/O Bottom line I/O About I/O Algorithmics 3 Errors Java choices Technical questions Q What kind of entity is an exception ? Algorithmics 3 Errors Java choices Technical questions Q What kind of entity is an exception ? A It’s an object. It’s a subclass of class Exception. Algorithmics 3 Errors Java choices Technical questions Q What kind of entity is an exception ? A It’s an object. It’s a subclass of class Exception. You can (and probably should) declare new exceptions. Algorithmics 3 Errors Java choices Technical questions Q What kind of entity is an exception ? A It’s an object. It’s a subclass of class Exception. You can (and probably should) declare new exceptions. Exception catching interacts with this subclass system. Algorithmics 3 Errors Java choices Exception checking What does this do ? p u b l i c c l a s s TestClass { p u b l i c void test () throws { return ; } p u b l i c v o i d moreTest ( ) { test (); } } Exception Algorithmics 3 Errors Java choices Exception checking What does this do ? p u b l i c c l a s s TestClass { p u b l i c void test () throws { return ; } p u b l i c v o i d moreTest ( ) { test (); } } Exception 1 error found: File: /tmp/TestClass.java [line: 9] Error: unreported exception java.lang.Exception; must be caught or declared to be thrown Algorithmics 3 Errors Java choices Exceptions are checked The compiler checks that all exceptions are either caught or declared. Algorithmics 3 Errors Java choices Exceptions are checked The compiler checks that all exceptions are either caught or declared. Well, not all exceptions. Not if they extend RuntimeException. Algorithmics 3 Errors Java choices Exceptions are checked The compiler checks that all exceptions are either caught or declared. Well, not all exceptions. Not if they extend RuntimeException. QWhat’s the point ? What’s the downside ? Algorithmics 3 Errors Java choices Exceptions are checked The compiler checks that all exceptions are either caught or declared. Well, not all exceptions. Not if they extend RuntimeException. QWhat’s the point ? What’s the downside ? AThe point is that otherwise, you would need to catch or declare an exception for each array access ! The downside is that it’s easy to abuse this. Just don’t. Algorithmics 3 Errors Java choices Uncaught exceptions Q What happens if you fail to catch an exception ? Algorithmics 3 Errors Java choices Uncaught exceptions Q What happens if you fail to catch an exception ? A Several answers: Algorithmics 3 Errors Java choices Uncaught exceptions Q What happens if you fail to catch an exception ? A Several answers: I You can’t. Algorithmics 3 Errors Java choices Uncaught exceptions Q What happens if you fail to catch an exception ? A Several answers: I You can’t. I Ok, you can if it’s a RuntimeException. Algorithmics 3 Errors Java choices Uncaught exceptions Q What happens if you fail to catch an exception ? A Several answers: I You can’t. I Ok, you can if it’s a RuntimeException. I Let’s look at an example. Algorithmics 3 Errors Java choices Exceptions as a debugging tool You’re encouraged to use exceptions to find out when you’re reaching a part of the program that shouldn’t be reachable. Algorithmics 3 I/O About I/O Program Errors About errors Exceptions Java choices A first look at I/O Bottom line I/O About I/O Algorithmics 3 I/O About I/O I/O ? I/O stands for Input/Output: getting data in the program / getting data out of the program. Algorithmics 3 I/O About I/O I/O ? I/O stands for Input/Output: getting data in the program / getting data out of the program. Input Reading a file. Receiving a message from the network. Receiving instructions from the keyboard. Output Writing a file. Sending a message to the network. Writing something on the screen. Algorithmics 3 I/O About I/O Do you I/O ? Truth be told, you’ve been programming I/O for some time. Algorithmics 3 I/O About I/O Do you I/O ? Truth be told, you’ve been programming I/O for some time. I’m talking of System.out.println(). Algorithmics 3 I/O About I/O Do you I/O ? Truth be told, you’ve been programming I/O for some time. I’m talking of System.out.println(). Let’s take a look at the documentation. Algorithmics 3 I/O About I/O Streams Input streams A construction able to provide a (potentially infinite) set of informations to your program. . Output streams A construction able to accept a (potentially infinite) set of informations coming from your program. . Algorithmics 3 I/O About I/O Streams Input streams A construction able to provide a (potentially infinite) set of informations to your program. In Java, they’re subclasses of InputStream. Output streams A construction able to accept a (potentially infinite) set of informations coming from your program. In Java, they’re subclasses of OutputStream. Let’s take a look at the documentation. Algorithmics 3 I/O About I/O Streams Input streams A construction able to provide a (potentially infinite) set of informations to your program. In Java, they’re subclasses of InputStream. Output streams A construction able to accept a (potentially infinite) set of informations coming from your program. In Java, they’re subclasses of OutputStream. Let’s take a look at the documentation. Note the exceptions. Algorithmics 3 I/O About I/O Most common streams Algorithmics 3 I/O About I/O Most common streams From the keyboard System.in Algorithmics 3 I/O About I/O Most common streams From the keyboard System.in To the screen System.out Algorithmics 3 I/O About I/O Most common streams From the keyboard System.in To the screen System.out From a file FileInputStream Algorithmics 3 I/O About I/O Most common streams From the keyboard System.in To the screen System.out From a file FileInputStream To a file FileOutputStream Algorithmics 3 I/O About I/O Enough for today Next time: putting files to good use.