65: STACK-BASED EXECUTION AND EXCEPTION PROPAGATION in Java

Maker QR Code 2d barcode in Java 65: STACK-BASED EXECUTION AND EXCEPTION PROPAGATION
65: STACK-BASED EXECUTION AND EXCEPTION PROPAGATION
QR Encoder In Java
Using Barcode creator for Java Control to generate, create QR Code image in Java applications.
Execution of Example 610 is illustrated in Figure 67 Each method execution is shown as a box with the local variables declared in the method The height of the box indicates how long a method is active Before the call to the method Systemoutprintln() at (6) in Figure 67, the stack trace comprises the three active methods: main(), printAverage() and computeAverage() The result 5 from the method computeAverage() is returned at (7) in Figure 67 The output from the program is in correspondence with the sequence of method calls in Figure 67 The program terminates normally, therefore this program behavior is called normal execution If the method call at (1) in Example 610
Encoding Bar Code In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
printAverage(100, 20); // (1a)
Bar Code Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
is replaced with
Paint QR Code ISO/IEC18004 In C#
Using Barcode creator for .NET Control to generate, create QR Code 2d barcode image in VS .NET applications.
printAverage(100, 0); // (1b)
Generate QR Code JIS X 0510 In .NET Framework
Using Barcode generation for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
and the program is run again, the output is as follows:
QR Code ISO/IEC18004 Maker In VS .NET
Using Barcode creation for .NET Control to generate, create QR image in Visual Studio .NET applications.
Computing average Exception in thread "main" javalangArithmeticException: / by zero at Average1computeAverage(Average1java:18) at Average1printAverage(Average1java:10) at Average1main(Average1java:5)
QR Code 2d Barcode Creator In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create Quick Response Code image in .NET applications.
Method Execution
GS1-128 Maker In Java
Using Barcode generation for Java Control to generate, create GTIN - 128 image in Java applications.
Average1 main() args = printAverage(100,20); // (1) totalSum = 100 totalNumber = 20 computeAverage(100,20); // (3) sum = 100 number = 20 println("Computing average"); // (6) 100/20 average = 5 println("Average = 100 / 20 = 5"); // (4) println("Exit printAverage()"); // (5) println("Exit main()"); // (2) 5 // (7) Systemout
Code 39 Extended Creation In Java
Using Barcode creation for Java Control to generate, create Code 39 Full ASCII image in Java applications.
Method execution
Printing Code 128C In Java
Using Barcode printer for Java Control to generate, create Code 128 Code Set B image in Java applications.
Output from the program:
Generating DataMatrix In Java
Using Barcode generation for Java Control to generate, create ECC200 image in Java applications.
Computing average Average = 100 / 20 = 5 Exit printAverage() Exit main()
Creating Bar Code In Java
Using Barcode maker for Java Control to generate, create bar code image in Java applications.
CHAPTER 6: CONTROL FLOW
Paint GS1 - 12 In Java
Using Barcode creation for Java Control to generate, create GS1 - 12 image in Java applications.
Figure 68 illustrates the program execution All goes well until the return statement at (7) in the method computeAverage() is executed An error condition occurs in calculating the expression sum/number, because integer division by 0 is an illegal operation This error condition is signaled by the JVM by throwing an ArithmeticException (see Exception Types on page 239) This exception is propagated by the JVM through the runtime stack as explained next Figure 68 illustrates the case where an exception is thrown and the program does not take any explicit action to deal with the exception In Figure 68, execution of the computeAverage() method is suspended at the point where the exception is thrown The execution of the return statement at (7) never gets completed Since this method does not have any code to deal with the exception, its execution is likewise terminated abruptly and its activation record popped We say that the method completes abruptly The exception is then offered to the method whose activation is now on the top of the stack (method printAverage()) This method does not have any code to deal with the exception either, so its execution completes abruptly The statements at (4) and (5) in the method printAverage() never get executed The exception now propagates to the last active method (method main()) This does not deal with the exception either The main() method also completes abruptly The
Code 128B Recognizer In VS .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Exception Propagation
Encoding Code 128 Code Set B In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
Average1 main() args = printAverage(100,0); // (1) totalSum = 100 totalNumber = 0 computeAverage(100,0); // (3) sum = 100 number = 0 println("Compute average"); // (6) 100/0 :ArithmeticExeception Systemout
Draw Code 128C In .NET
Using Barcode printer for .NET framework Control to generate, create Code 128 Code Set C image in .NET applications.
Exception propagation
Printing Code 3/9 In .NET Framework
Using Barcode maker for Visual Studio .NET Control to generate, create Code 39 image in VS .NET applications.
"/ by zero"
Code39 Recognizer In .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
Output on the terminal:
Painting Code 39 In C#.NET
Using Barcode printer for .NET framework Control to generate, create Code 39 image in .NET framework applications.
Output from the program
Draw Bar Code In .NET
Using Barcode drawer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Output from the standard exception handler
Compute average Exception in thread "main" javalangArithmeticException: / by zero at Average1computeAverage(Average1java:18) Stack Trace at Average1printAverage(Average1java:10) at Average1main(Average1java:5)
Class Name Method Name Filename Line number where call to the next method occurs
66: EXCEPTION TYPES
statement at (2) in the main() method never gets executed Since the exception is not caught by any of the active methods, it is dealt with by the main thread s default exception handler The default exception handler usually prints the name of the exception, with an explanatory message, followed by a printout of the stack trace at the time the exception was thrown An uncaught exception results in the death of the thread in which the exception occurred If an exception is thrown during the evaluation of the left-hand operand of a binary expression, then the right-hand operand is not evaluated Similarly, if an exception is thrown during the evaluation of a list of expressions (eg, a list of actual parameters in a method call), evaluation of the rest of the list is skipped If the line numbers in the stack trace are not printed in the output as shown previously, use the following command to run the program: