1 / 57

Chapter 3:

Chapter 3:. Control Flow and Exception Handling. Expressions & Operator Precedence. Variables and operators are the basic building blocks of programs. Literals, variables and operators are combined to form expressions. Expressions perform computations and return values.

azizi
Download Presentation

Chapter 3:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: Comp321: Object Oriented Programming Control Flow and Exception Handling

  2. Expressions & Operator Precedence • Variables and operators are the basic building blocks of programs. Literals, variables and operators are combined to form expressions. Expressions perform computations and return values. • The data type of the return value of an expression depends on the elements used in the expression. An operator returns a value, so the use of an operator is an expression. • Some examples of expressions are shown in bold in the code snippets below: inti = 10; char aChar = 's'; System.out.println ("The value is " + i); If (Character.isUpperCase (aChar)) { ………… } • As in other programming languages, there is an order of precedence for the Java operators. The order is shown in the table below – the operators are listed with highest precedence at the top of the table and the lowest at the bottom. Comp321: Object Oriented Programming

  3. Comp321: Object Oriented Programming

  4. Statements • A statement is a complete unit of execution. Any expression that has a side-effect, i.e. it changes the state of the program in some way, can be used as a statement simply by putting a semi-colon after it. • These are assignments, increments and decrements, method calls and object creation. • For example: i = 10; //assignment i++; //increment System.out.println ("Value of i is: + i); //method call Integer integerObject = new Integer(4); //object creation • Variable declarations are also statements eg: float f = 1.234; • Control flow statements regulate the order in which statements get executed. These include if and for statements.  • A block is a group of 0 or more statements between curly braces ({}) and can be used anywhere that a single statement is allowed. Blocks are used to group statements together e.g. the statements to execute when the condition for an if statement is true. Comp321: Object Oriented Programming

  5. Control Of Flow Statements • Control flow statements govern the flow of control in a program during execution, that is, the order in which statements are executed in a running program. There are three main categories of control flow statements that are discussed in this chapter: • Selection statements: if, if-else and switch. • Iteration statements: while, do-while and for. • Transfer statements: break, continue, return, try-catch-finally and assert. Comp321: Object Oriented Programming

  6. if (Selection Statement) • The if statement is a fundamental control statement that is used to make decisions i.e. to execute statements conditionally. The basic syntax for If in Java is as follows: if ( expression ) { statement(s) } • The expression is any conditional expression that returns a boolean value (true or false). • There must be brackets around the condition expression. • The statements to execute if the expression evaluates to true should be enclosed in a block. • In this form, if the expression evaluates to false, execution simply moves to the next line of code after the block. Comp321: Object Oriented Programming

  7. if-else (Selection Statement) • The if statement can be extended to include an else clause, which is the block of statements to execute if the expression evaluates to false.  if ( expression ) { //code to execute if expression is true statement(s) } else { //code to execute if expression is false statement(s)  } Comp321: Object Oriented Programming

  8. if-else (Selection Statement)(cont) • If there are more than two possible blocks of code to execute, the else/if clause can be used: if ( expression1 ){ //code to execute if expression is true statement(s) } else if ( expression2 ){ //code to execute if expression2 is true statement(s) } else { //code to execute if neither expression 1 nor expression2 is true statement(s) } • The if statement can have as many else/if clauses as required, but can have only one else clause. Using else/if is equivalent to nesting if/else statements – but using else/if makes the code clearer and easier to read. Nested if/else statements should be clearly defined with parentheses to avoid confusion and unexpected results. Comp321: Object Oriented Programming

  9. Switch (Selection Statement) • However, if there are multiple branching options, a better alternative to if/else is to use a switch statement. Consider a case where the conditional expression is checking the value of a particular variable e.g. using an if/else to allocate a grade based on an exam mark. The same variable will be evaluated for each block of code – this is inefficient. • A switch statement evaluates the value of the variable once and then decides which branch to take based on the value. • The expression evaluated for the switch must return an integer value. Control jumps to the entry point specified by that value – each entry point is marked with the case keyword. The end of the block of code to execute for a given value must be marked with the break keyword (otherwise the code will continue executing to the end of the switch block). Comp321: Object Oriented Programming

  10. Switch (SelectionStatement)(cont) switch ( integer_expression ) { case int_value_1: statements; break; case int_value_2: statements; break; case int_value_3: statements; break; default: statements; break; } • The default case is optional, and is executed if the value of integer_expression does not match any of the cases. Comp321: Object Oriented Programming

  11. Switch (Selection Statement)(cont) • More than one case clause can label the same set of statements e.g. if, in the example above, if the cases for int_value_1 and int_value_2 result in the same statements being executed: case int_value_1: case int_value_2: statements; break; • The expression can be a char type – because a char is represented by the corresponding Unicode value, which is an integer. • There are circumstances where an if/else must be used – because the switch can only make decisions based on an integer value. The if/else is more flexible because the test expression can be any expression that returns a boolean value. Comp321: Object Oriented Programming

  12. Ternary(Conditional) Operator • Java provides a conditional operator that is similar to an if/else statement. • For example, consider the following if/else statement:  if ( i > j ) { System.out.println ("max is i"); } else { System.out.println ("max is j"); } • Using the conditional operator, ?, this can also be written as: (i > j ) ?System.out.println ("max is i") :System.out.println ("max is j") • Of it can be written like this: System.out.println ("max is " + ( (i > j ) ?i: j)); • The condition appears before the ?. The statement to execute if the condition is true appears after the ? and before the :. The statement to execute if the condition is false appears after the :. Comp321: Object Oriented Programming

  13. for(Iteration Statements) • The for statement allows a piece of code to be executed a specified number of times, in an iterative loop. • The syntax is: for ( initialisation ; termination ; update ) { statement_block   } • The initialisation is executed at the beginning of the loop. It is usually used to set the initial value of the counter used to control loop execution. • The termination is the test that determines when to exit the loop. It is an expression that is evaluated at the beginning of each iteration. When it is true, the loop terminates. • The update is an expression that is evaluated at the end of each iteration. It usually increments or decrements the counter used to control loop execution.  • For example:  System.out.println ("Countdown from 10…\n"); for (inti=10 ; i >= 0 10; i--) { System.out.println (i); } Comp321: Object Oriented Programming

  14. for(Iteration Statements)(cont) • The for loop is frequently used to iterate through the values in an array. • The intialization expression can be used to declare a local variable – the scope of this variable is the for statement and the block it controls – so it can be used in the termination and update expressions as well as within the block.   • String[] monthsArray = {"January", "February","March","April","May","June","July","August","September","October","November","December"}; //display the months for (inti=0 ; i < monthsArray.length ; i++ } { System.out.println (monthsArray[i] + "\n"); } Comp321: Object Oriented Programming

  15. for(Iteration Statements)(cont) • The following code creates an int array and sums the elements in the array. int sum = 0; int[] array = {12, 23, 5, 7, 19}; for (int index = 0; index < array.length; index++) // (1) sum += array[index]; • The loop variable index is declared and initialized in the <initialization> section of the loop. It is incremented in the <increment expression> section. The for loop defines a local block such that the scope of this declaration is the for block, which comprises the <initialization>, the <loop condition>, the <loop body> and the <increment expression> sections. • The loop at (1) showed how a declaration statement can be specified in the <initialization> section. Such a declaration statement can also specify a comma-separated list of variables. • for (inti = 0, j = 1, k = 2; ... ; ...) ...; // (2) The variables i, j, and k in the declaration statement all have type int. All variables declared in the <initialization> section are local variables in the for block and obey the scope rules for local blocks. However, note that the following code will not compile, as variable declarations of different types (in this case, int and String) require declaration statements that are terminated by semicolons. Comp321: Object Oriented Programming

  16. for(Iteration Statements)(cont) for (inti = 0, String str = "@"; ... ; ...) ...; // (3) • Compile time error. The <initialization> section can also be a comma-separated list of expression statements. For example, the loop at (2) can be rewritten by factoring out the variable declaration. inti, j, k; // Variable declaration for (i = 0, j = 1, k = 2; ... ; ...) ...; // (4) Just initialization • The <initialization> section is now a comma-separated list of three expressions. The expressions in such a list are always evaluated from left to right. Note that the variables i, j, and k are now obviously not local to the loop. • Declaration statements cannot be mixed with expression statements in the <initialization> section, as is the case at (5) in the following example. Factoring out the variable declaration, as at (6), leaves a legal comma-separated list of expression statements only. • // (5) Not legal and ugly. for (inti = 0, System.out.println("This won't do!"); flag; i++) { // loop body }  Comp321: Object Oriented Programming

  17. for(Iteration Statements)(cont) • // (6) Legal, but still ugly. inti; for (i = 0, System.out.println("This is legal!"); flag; i++) { // loop body } • The <increment expression> can also be a comma-separated list of expression statements. The following code specifies a for loop that has a comma-separated list of three variables in the <initialization> section, and a comma-separated list of two expressions in the <increment expression> section. • // Legal usage but not recommended. int[][] sqMatrix = { {3, 4, 6}, {5, 7, 4}, {5, 8, 9} }; for (inti = 0, j = sqMatrix[0].length - 1, asymDiagonal = 0; // initialization i < sqMatrix.length; // loop condition i++, j--) // increment expression asymDiagonal += sqMatrix[i][j]; // loop body • All the sections in the for-header are optional. Any one of them can be left empty, but the two semicolons are mandatory. In particular, leaving out the <loop condition> signifies that the loop condition is true. The "crab", (;;), is commonly used to construct an infinite loop, where termination is presumably achieved through code in the loop body (see next section on transfer statements): • for (;;) Java.programming(); // Infinite loop

  18. While(Iteration Statements) • The while statement is another looping mechanism, that allows a block of code to be executed repetitively while a specified condition is true. • A for loop is used when the programmer knows how many times the code block is to be executed. With a while loop, the number of times the loop is executed can vary with each time the code is run. •  Syntax: while ( expression ) { statement_block } • The expression is evaluated at the beginning of each repetition of the loop. • Expression must return a boolean value. If it is true, the code in the statement_block is executed. If it is false, execution moves to the next statement in the program, after the statement_block.

  19. While(Iteration Statements) • For example: //a simple while loop to count up to 10 int counter = 0; while ( counter <= 10 ) { System.out.println (counter); counter++; } Comp321: Object Oriented Programming

  20. do-while(Iteration Statements) • A do-while loop is similar, except that the condition is evaluated at the end of each repetition of the statement_block: • Syntax:  do { statement_block } while ( expression ) • The difference between the while and the do-while loops is that the statement_block in the do-while is guaranteed to be executed at least once – because the expression is not evaluated at the end of the loop. The statement_block in a while loop may not execute at all, if expression evaluates to false on the first loop. Comp321: Object Oriented Programming

  21. do-while(Iteration Statements)(cont) • The same loop as a do-while:  • //a simple while loop to count up to 10 • int counter = 0; • do { • System.out.println (counter); • counter++; • } • while ( counter <= 10 ); Comp321: Object Oriented Programming

  22. Transfer Statements • Java provides six language constructs for transferring control in a program: • break • continue • return • try-catch-finally • throw • Assert • Note that Java does not have a goto statement, although goto is a reserved word. Comp321: Object Oriented Programming

  23. Break(Transfer Statements) • The break statement comes in two forms: the unlabeled and the labeled form. break; // the unlabeled form break <label>; // the labeled form • The unlabeled break statement terminates loops (for, while, do-while) and switch statements which contain the break statement, and transfers control out of the current context (i.e., the closest enclosing block). The rest of the statement body is skipped, terminating the enclosing statement, with execution continuing after this statement. • A labeled break statement can be used to terminate any labeled statement that contains the break statement. Control is then transferred to the statement following the enclosing labeled statement. Comp321: Object Oriented Programming

  24. Break(Transfer Statements)(cont) • Example   for (inti = 1; i <= 5; ++i) { if (i == 4){ break; // (1) Terminate loop. Control to (2). } // Rest of loop body skipped when i gets the value 4 System.out.println(i); } // end for // (2) Continue here.  Output from the program: 1 2 3 Inner for loop:01default: 2 Comp321: Object Oriented Programming

  25. Break(Transfer Statements)(cont) Example int n = 2; switch (n) { case 1: System.out.println(n); break; case 2: System.out.println("Inner for loop: "); for (int j = 0; j < n; j++) if (j == 2) break; // (3) Terminate loop. Control to (4). else System.out.println(j); default: System.out.println("default: " + n); // (4) Continue here. } Output: Inner for loop: 0 1 default: 2 Comp321: Object Oriented Programming

  26. Break(Transfer Statements)(cont) • In the case of a labeled block, the rest of the block is skipped and execution continues with the statement following the block: • Eg. out:{ // (1) Labeled block // ... if (j == 10) break out; // (2) Terminate block. Control to (3). System.out.println(j); // Rest of the block not executed if j == 10. // ... }// (3) Continue here. Comp321: Object Oriented Programming

  27. continue(Transfer Statements) • Like the break statement, the continue statement also comes in two forms: the unlabeled and the labeled form. • continue; // the unlabeled form • continue <label>; // the labeled form • The continue statement can only be used in a for, while, or do-while loop to prematurely stop the current iteration of the loop body and proceed with the next iteration, if possible. In the case of the while and do-while loops, the rest of the loop body is skipped, that is, stopping the current iteration, with execution continuing with the <loop condition>. In the case of the for loop, the rest of the loop body is skipped, with execution continuing with the <increment expression>. Comp321: Object Oriented Programming

  28. continue(Transfer Statements)(cont) for (inti = 1; i <= 5; ++i) { if (i == 4) { continue; // (1) Control to (2). } // Rest of loop body skipped when i has the value 4. System.out.println(i); // (2). Continue with increment expression. } // end for Output from the program: 1 2 3 Comp321: Object Oriented Programming

  29. continue(Transfer Statements)(cont) • A labeled continue statement must occur within a labeled loop that has the same label. Execution of the labeled continue statement then transfers control to the end of that enclosing labeled loop. Comp321: Object Oriented Programming

  30. return(Transfer Statements) The return statement is used to stop execution of a method and transfer control back to the calling code (a.k.a. the caller). The usage of the two forms of the return statement is dictated by whether it is used in a void or a non-void method. The first form does not return any value to the calling code, but the second form does. Note that the keyword void does not represent any type. The <expression> must evaluate to a primitive value or a reference value, and its type must be assignable to the return type in the method prototype. Comp321: Object Oriented Programming

  31. return(Transfer Statements)(cont) • Eg public class ReturnDemo {   public static void main (String[] args) { // (1) void method can use return. if (args.length == 0) return; output(checkValue(args.length)); }   static void output(int value) { // (2) void method need not use return. System.out.println(value); return 'a'; // Not OK. Cannot return a value. } static intcheckValue(inti) { // (3) non-void method must return a value. if (i > 3) return i; // OK. else return 2.0; // Not OK. double not assignable to int. } } Comp321: Object Oriented Programming

  32. Exceptions • Runtime errors that occur in Java are called 'exceptions'. These are distinct from errors that are found at the time of compiling. • Errors that are detected by the compiler are generally syntax errors. These include errors such as: • Missing semi-colons at the end of statements • Missing or extra brackets or curly braces in classes and methods • Misspelling of identifiers and keywords • Use of undeclared variables • Incompatible types in assignments and initialisations • References to non-existent objects • Use of = instead of the = = operator Comp321: Object Oriented Programming

  33. Exceptions(cont) • However, even if a program is syntactically correct and compiles, it can still produce errors at runtime. These types of errors include the following: • Dividing an integer by 0 • Accessing an element that is out of bounds in an array • Trying to store a value into an array of an incompatible class of type • Passing a parameter that is not in a valid range or value for a method • Using a null object reference to access a method or a variable • Converting an invalid string to a number • Accessing a character that is out of bounds in a string Comp321: Object Oriented Programming

  34. Exceptions(cont) • The different kinds of exceptions  • Checked Exceptions : Environmental error that cannot necessarily be detected by testing; e.g. disk full, broken socket, database unavailable, etc. • Errors : Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc. • Runtime Exceptions : Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument, etc. • Checked exceptions must be handled at compile time. Runtime exceptions do not need to be. Errors often cannot be. Comp321: Object Oriented Programming

  35. Exceptions(cont) • There are many other possible runtime errors. • When the interpreter encounters such an error, it generates an error message and aborts the program. For example, the class below will compile because it is syntactically correct public class DemoError1 { public static void main (String[] args){ int a = 10; int b = 5; int c = 5; int x = a/(b-c); //division by zero System.out.println ("x = " + x); } } Comp321: Object Oriented Programming

  36. Exceptions(cont) • But when the previous class is run, it produces a runtime error because there is a division by zero. The error message reads like this:  Exception in thread "main" java.lang.ArithmeticException: / by zeroat DemoError1.main(DemoError1.java:9) • The text in bold is a Java exception object. A Java exception is a condition that is caused by a runtime error in a program. The interpreter creates the exception object and throws it i.e. informs the program caller that an error has occurred. • If the exception is not caught and handled properly in the program code, the interpreter displays a message like the one shown above and halts execution of the program. • In order to have the program continue executing, the code has to catch the exception object thrown by the error condition and then take some corrective action. This is known as exception handling. • Some common exceptions are listed in the table below.

  37. Comp321: Object Oriented Programming

  38. Exceptions(cont) • There are many other exceptions, but the previous are common types of exception that you are likely to come across. • Why use exceptions instead of return values? • Forces error checking • Cleans up your code by separating the normal case from the exceptional case. (The code isn't littered with a lot of if-else blocks checking return values.) • Low overhead for non-exceptional case • Traditional programming languages set flags or return bad values like -1 to indicate problems. Programmers often don't check these values. • Java throws Exception objects to indicate a problem. These cannot be ignored. • The exception mechanism is built around the throw-and-catch paradigm. To throw an exception is to signal that an unexpected error condition has occurred. To catch an exception is to take appropriate action to deal with the exception. An exception is caught by an exception handler, and the exception need not be caught in the same context that it was thrown in. The runtime behavior of the program determines which exceptions are thrown and how they are caught. The throw-and-catch principle is embedded in the try-catch-finally construct.

  39. Exceptions(cont) • Exception Methods : Mostly exceptions just serve as signals. They tend not to have a lot of methods of their own, and those they have are rarely invoked directly. The two most commonly used are toString() and printStackTrace(). • public String getMessage() • public String getLocalizedMessage() • public String toString() • public void printStackTrace() • public void printStackTrace(PrintStream s) • public void printStackTrace(PrintWriter s) • public ThrowablefillInStackTrace() • All of these are inherited from java.lang.Throwable as are pretty much all other methods in most exception classes. Comp321: Object Oriented Programming

  40. Try/catch • The syntax for catching exceptions and handling them is as follows. It consists of a try block, which throws the exception object, followed by a catch block, which catches and handles the exception.  try { Statement that may cause the exception } catch ( Exception_typeparameter_name) { Statement that handles the exception } • The try block can have one or more statements that could generate an exception. If any one of the statements causes an exception, execution of the remaining statements in the block is skipped and execution jumps to the catch block. • The catch block contains one or more statements to handle the exception. The catch statement takes one parameter – a reference to the exception object that was thrown by the try block. If the type of the thrown exception matches the type of the parameter, then the exception will be caught and the statements in the catch block will be executed. The parameter_name can be used to refer to the exception object within the catch block. • Any statements following the catch block are then executed – i.e. execution of the program does not stop when an exception is handled.

  41. Try/catch(cont) • For example, to catch the ArithmeticException that is thrown by the example given above: public class DemoError1 {  public static void main (String[] args){ int a = 10; int b = 5; int c = 5; int x; try { x = a/(b-c); //division by zero System.out.println ("x = " + x); }   catch (ArithmeticException e) { System.out.println ("Error: division by zero"); } }  } Comp321: Object Oriented Programming

  42. Try/catch(cont) • If a block of statements could potentially generate multiple different types of exception, different catch blocks can be written to catch each type. • When an exception is generated in the try block, the interpreter treats the multiple catch statements like cases in a switch – it finds the catch statement whose parameter matches the type of the thrown exception object. • If you are unsure what exception is being thrown by a block of code, put a catch block for exceptions of type Exception – as all exceptions are sub-classes of this, this block will catch any exception. However, this should only be used as a failsafe, after catching other, known exception types, or to debug the program. Comp321: Object Oriented Programming

  43. Try/catch(cont) try { statement; } catch (Exception_type1 e) { statement; } catch (Exception_type2 e) { statement; } catch (Exception_type3 e) { statement; } //the last catch block should catch all other types of exception catch (Exception e) { statement; } Comp321: Object Oriented Programming

  44. Try/catch(cont) public class HelloThere {   public static void main(String[] args) { int repeat; try { repeat = Integer.parseInt(args[0]); } catch (ArrayIndexOutOfBoundsException e) { // pick a default value repeat = 1; } catch (NumberFormatException e) { // print an error message System.err.println("Usage: java HelloThererepeat_count" ); System.err.println( "where repeat_count is the number of times to say Hello" ); System.err.println("and given as an integer like 1 or 7" ); return; } for (inti = 0; i < repeat; i++) { System.out.println("Hello"); } } } Comp321: Object Oriented Programming

  45. Try/catch(cont) • The statements in a catch block should be designed to cope with the type of exception that occurred, and to recover from the exception if possible. In some cases, this may only be to inform the user, if the program is interactive. In other cases, it may require logging the error to an error log (i.e. writing to a file) and/or taking some corrective action. • Note that it is not necessary to put any processing statements in the block – it can be empty. In that case, the catch block is there simply to avoid the program execution being stopped. An empty block is created by putting a semi-colon after the catch statement. For example:  catch (Exception e); • It is not necessary to have a catch block for every possible exception. If an exception is thrown by a method and not caught in that method, it propagates up and is caught by the method that invoked that method. It may be better sometimes to allow the invoking method to catch the exception and handle it.

  46. Try/catch(cont) • The Throwablesuperclass has some useful methods that can be used to get more information about an exception. • The getMessage() method returns any error message associated with the exception. • The toString() method returns a string representation of the exception, including the full name of the exception class (e.g. java.lang.ArithmeticException) and the error message. • These can be used for debugging purposes or to display more information to the user of the program. • For example:  try { statement; } catch(Exception e) { System.out.println ("Error message:\n" + e.getMessage() + "\n" + e.toString()); } Comp321: Object Oriented Programming

  47. Try/catch(cont) • What can you do with an exception once you've caught it? • Fix the problem and try again. • Do something else instead. • Exit the application with System.exit() • Rethrow the exception. • Throw a new exception. • Return a default value (in a non-void method). • Eat the exception and return from the method (in a void method). • Eat the exception and continue in the same method (Rare and dangerous. Be very careful if you do this. Novices almost always do this for the wrong reasons. Do not simply to avoid dealing with the exception. Generally you should only do this if you can logically guarantee that the exception will never be thrown or if the statements inside the try block do not need to be executed correctly in order for the following code to run.) • Printing an error message by itself is generally not an acceptable response to an exception. Comp321: Object Oriented Programming

  48. Finally • The finally statement can be used after a try block or after a try/catch block. • Generally, the code in the finally block is used to clean up after the code in the try block e.g. close any open files, release system resources, display some output to the user. • The finally block is guaranteed to be executed if any part of the try block is executed, if the try block executes with no exceptions or with exceptions. Even if an exception is thrown and there is no catch statement for it, the finally block will execute before program execution ends.  • Using the finally block means that the programmer does not have to put the same clean-up code in both the try and catch blocks. Even if there are multiple catch blocks, there is only one finally block.  Comp321: Object Oriented Programming

  49. Finally(cont) • A finally block can be added after a try block, if there are no catch blocks. Or it can be added after the catch block(s). try { statement; } catch (Exception_type e) { statement; } finally { statement; //clean up } Comp321: Object Oriented Programming

  50. Throwing Exceptions • The classes of exception described above are all runtime exceptions. Because these can happen often, they are automatically thrown by methods. These are also called 'unchecked exceptions'. • There are some types of exception that need to be explicitly thrown by methods. These are called 'checked exceptions'. They are checked because the compiler checks to make sure they have been declared or handled. • Unchecked exceptions, on the other hand, do not have to be declared or handled. • An example of a checked exception is IOException. This occurs when there are problems with reading or writing to files or standard input/output. If a method contains code that could generated an IOException, the compiler will produce an error. Comp321: Object Oriented Programming

More Related