30 Pages

Exception

Course: PRO 404, Fall 2009
School: CSU Fullerton
Rating:
 
 
 
 
 

Word Count: 1163

Document Preview

An Exception exception is an object that is generated at runtime to describe a problem encountered during the execution of a program. Some causes for an exception are integer division-by-zero, array index out-of-bounds, illegal cast, interrupted I/O operation, missing file, incorrect number format etc. Exception Handling Java exception handling enables a program to catch all exceptions, or to catch all...

Register Now

Unformatted Document Excerpt

Coursehero >> California >> CSU Fullerton >> PRO 404

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
An Exception exception is an object that is generated at runtime to describe a problem encountered during the execution of a program. Some causes for an exception are integer division-by-zero, array index out-of-bounds, illegal cast, interrupted I/O operation, missing file, incorrect number format etc. Exception Handling Java exception handling enables a program to catch all exceptions, or to catch all exceptions of a certain type, or to catch all exceptions of related types. This makes programs more robust by reducing the likelihood that errors will not be caught by a program. Exception Handling Exception handling is provided to enable programs to catch and handle errors rather than letting them occur and suffering the consequences. Exception handling is designed for dealing with synchronous errors such as an attempt to divide by zero. Exception Handling Exception handling is not designed to deal with asynchronous events such as disk I/O completions, network message arrivals, mouse clicks, keystrokes and the like; These are best handled through other means, such as Java event listeners. When Exception Handling Should Be Used To process only exceptional situation where a method is unable to complete its task for reasons it cannot control. To process exceptions form program components that are not geared to handling those exceptions directly. When Exception Handling Should Be Used To process exceptions from software components such as methods, libraries and classes that are likely to be widely used, and where those components cannot handle their own exceptions. On large projects to handle exceptions in a uniform manner project wide. An Example of Exception Handling Let us do the first example............ The output from this program is: Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideByZero.d(DivideByZero.java:21) at DivideByZero.c(DivideByZero.java:15) at DivideByZero.b(DivideByZero.java:11) at DivideByZero.a(DivideByZero.java:7) at DivideByZero.main(DivideByZero.java:3) Example You can see that an arithmetic exception object was generated at line 21. It was handled by the default exception handling mechanism. Information about the exception is displayed, and execution of the application is stopped. catch, try and finally block The Java language allows you to handle exceptions that occur during execution of a program. This is done by using the following syntax: catch, try and finally block Try{ //try block } Catch(Exception Type1 param1){ //exception handling block} ... catch(Exception TypeN param3){ //exception handling block } Finally{ //finally block } catch, try and finally block The try statement contains a block of statements enclosed by braces. This is the code you want to monitor for exceptions. If a problem occurs during its execution, an exception is thrown. catch, try and finally block Immediately following the try block is a sequence of catch blocks. Each of these begins with the catch keyword. An argument is passed to each catch block. That argument is the exception object that contains information about the problem. catch, try and finally block If a problem occurs during execution of the try block, the java Virtual Machine immediately stops executing the try block and looks for a catch block that can process that type of exception. Any remaining statements in the try block are not executed. catch, try and finally block The search begins at the first catch block. If the type of the exception object matches the type of the catch block parameter, those statements are executed. Otherwise, the remaining catch clauses are examined in sequence for a type match. catch, and try finally block One of the biggest advantages of the Java exception handling mechanism is that it allows you to write software that is better organized and easier to understand. You no longer need to distribute code to check and handle errors throughout your program. Instead , a try block can be written and , if problems occur during its execution, control proceeds in a well-defined manner to the appropriate catch block. catch, try and finally block When a catch block completes execution , control passes to the statements in the finally block. The Java compiler ensures that the finally block is executed in all circumstances. When a try block completes without problems, the finally block executes. Even if a return statement is included in a try block, ther compiler ensures that the finally block is executed before the current method returns. catch, try and finally block The finally block is optional. Each try block must have at least one catch or finally block. Oterwise a compiler error occurs. Example 2 The example Divider.java illustrates the flow of control in a program with exception handling. The application accepts two strings in integer format as its command-line arguments. Example 2 The first number is divided by the second number and the result is displayed. There are catch blocks to handle different types of exceptions that can occur. Example 2 If a division-by-zero is attempted, an ArithmeticException occurs. An ArrayIndexOutOfBoundsException occurs if one or both of the command-line arguments is missing. Example 2 If an argument is not formatted as an integer, a NumberFormatException occurs. Try executing this program with these different conditions and observe the results. Example2 Notice that the finally block always executes. Also observe that the call to println( ) after the division never executes if an exception occurs. Throwing an Exception The Java language has many exceptions that it can throw: From the Java Virtual Machine itself when a runtime error occurs. Java developers may also throw exceptions from their own methods Additionally, you can write your own exception classes Throwing an Exception In the previous examples,...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

CSU Fullerton - PRO - 404
PRO 404 AAHMED , WAFA MAHMOUD () BARTOLOME , CARL () CASASFRANCO , GUSTAVO ADOLFO () CHANA , RANDEEP SINGH () DRAGUN , SASHA () GILL , DALJIT () GIORCELLI , VALENTINA () JAKOVLJEVIC , ROBERT () KANG , GURJOT SINGH () KATARIA , PARMILA () KOK SHUN ,
CSU Fullerton - PRO - 404
PRO 404 BABRAHAMYAN , MARINE () AKSENTIC , BORIS () AVDAGIC , SANJA () BERHANU , LEIKUN WEDAJE () BLAIR , PETER () BRAHMENDRA , SUKANYA () ENGALLA , ORLANDO AQUINO () GARCIA-LAZ , GLORIA () GHANAD POUR , MARJAN () GUENTHER , NOLAN JAMES () HASIN , H
CSU Fullerton - PRO - 404
>jli7 LbjbjUU"7|7|Ll(2$P pp:Q@E^V(.i0q,(Assignment2 = 10%PRO404 - SectionABDue : Nov 2, 2001 at 11.59pmWrite an application that displays the calendar for the current month and year. It should appear as shown in the following illustration.The mon
CSU Fullerton - PRO - 404
Course Information Professor: Prabin Dutta Email:Prabin.dutta@senecac.on.ca Homepage:http:/cs.senecac.on.ca/~pduttaAssignment/Exam Important dates: Assignment 1: Assignment 2: Assignment 3: Midterm Exam: Final Exam:Text Book Java How
CSU Fullerton - PRO - 404
<HTML><applet code="ComTest1.class" height=200 width=300></applet></HTML>
CSU Fullerton - PRO - 404
<HTML><applet code="ComTest2.class" height=200 width=300></applet></HTML>
Michigan State University - WRIGH - 238
1. Brief description of case: The HMV is a computer systems corporation, with over 107,000 employees. Using their own products, they house two of the world's largest networking systems for their employees to use. The BBS (bulletin board service) is f
UCSB - ECE - 146
ECE 146B Gibson Homework No. 2 1. Problem 8.1 in the text. 2. Problem 8.2. 3. Problem 8.5. 4. Problem 8.17.Spring 2009 Due: April 14, 2009
UCSB - ECE - 146
ECE 146B Gibson Homework No. 3 1. Problem 8.5 in the text. 2. Problem 8.10. 3. Problem 8.12. 4. Problem 8.13.Spring 2009 Due: April 21, 2009
UCSB - ECE - 146
ECE 146B Gibson Homework No. 5 1. Problem 2.66 in the text. 2. Problem 9.2. 3. Problem 9.4. 4. Problem 9.5. 5. Problem 9.8.Spring 2009 Due: May 19, 2009
UCSB - ECE - 146
ECE 146B Gibson Homework No. 6 1. Problem 10.1 in the text. 2. Problem 10.5. 3. Problem 10.7. 4. Problem 10.9. 5. Problem 10.13.Spring 2009 Due: May 28, 2009
UCSB - ECE - 146
ECE 146B: Digital Communication Theory and Techniques Lab 3: M -ary Phase Shift Keying (PSK) and M -ary Quadrature Amplitude Modulation (QAM)Lab Report Due: 5:00 p.m., Friday, May 22, 2009 (Place in the ECE 146B Homework Box on the 3rd Floor of Haro
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Mathematical Principles of Computing I Autumn 2004 Instructor: Ed Hong edhong@u.washington.edu Pinkerton 310 (253) 692-4659 Office hours: M 4-5:pm, W 2-3 pm or by appointment (use e-mail). Class Time and Pla
Washington - TCSS - 342
TCSS 342 In-class exercise (10/04/04) (revised 10/5) Exercise 1: What is the running time (expressed in Big-Oh notation) of the following code fragment? sum = 0; for (i = 0; i < n; i+) { for (j = 0; j < n * n; j+) { sum+; } }Exercise 2: What is the
Washington - TCSS - 342
TCSS 342 In-class exercise (10/04/04) (revised 10/5) SOLUTIONS. Exercise 1: What is the running time (expressed in Big-Oh notation) of the following code fragment? sum = 0; for (i = 0; i < n; i+) { for (j = 0; j < n * n; j+) { sum+; } }Clearly the l
Washington - TCSS - 342
In an attempt to garner more business, a restaurant has decided to hold a sweepstakes, and as a grand prize, give away a trip to Hawaii at the end of the year. The rules for the sweepstakes are as follows: Customers enter the sweepstakes by filling a
Washington - TCSS - 342
The sorting game.an application of Queues and an efficient implementation. Radix Sort We wish to sort an array with n positive integers. Assume all integers have at most 10 digits. Solution: a. Create 10 queues labeled Q_0, ., Q_9. b. Scan the array
Washington - TCSS - 342
TCSS 342, Winter 2006 Lecture NotesTrees Binary Trees Binary Search Treesversion 1.01Chapter ObjectivesLearn about tree structures Learn about how to implement tree structures definition traversal operationsLearn about binary
Washington - TCSS - 342
TCSS 342, Winter 2006 Lecture NotesBalanced Binary Search Trees1 TCSS 342B v1.0 Learn about balanced binary search trees AVL trees red-black treesObjectives Talk about why they work Talk about their run-time performance.2 TCSS 342B v1.0
Washington - TCSS - 342
In class drill The following 20 numbers are inserted into an binary search tree. 297 789 597 408 723 344 695 91 289 981 27 360 65 436 700 920 99 925 6151. Build this BST.2. Insert into the BST the following numbers:256006056103. Delete
Washington - TCSS - 342
In class drill: 1. Show how mergesort would sort the following numbers: 297 597 723 695 289 27 65 700 99 6152. Do the same for quicksort.3. Also try insertion sort and selection sort on the same list of numbers.
Washington - TCSS - 342
TCSS 342, Winter 2006 Lecture NotesJava's Set ADT Java's Map ADT Sample Word Finding/Counting applicationversion 1.0 1ObjectivesLearn more about Java's Collection API Learn how Set and Map are used Discuss the basics of how they are im
Washington - TCSS - 342
Hashing practice sheet. Using the standard java hash function, the hash code for a String object is computed ass[0]*31^(n-1) + s[1]*31^(n-2) + . + s[n-1]using int arithmetic, where s[i] is the ith character of the string, n is the length of the st
Washington - TCSS - 342
In class self study.Heaps: Insert the following integers into a priority queue implemented as a heap, using top down insertion. Count how many comparisons and swaps you needed to execute. Draw both the complete binary tree and the corresponding arr
Washington - TCSS - 342
A Red-Black Tree15 3 2 5 10 11 20 25 2630 50 40 55 59 60 65 6670 85 80 81 90 91 115 7102-3-4 Trees and Red-Black Trees. Converting a Red-Black tree to a 2-3-4 tree and vice-versa.Unchanged5 22 4555 7 47646949The 2-3-4 tr
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Mathematical Principles of Computing I Winter 2006 Instructor: Ed Hong edhong@u.washington.edu Pinkerton 310 (253) 692-4659 Office hours: Tuesday 11AM-12Noon, Wednesday 2PM-4PM, or by appointment (use e-mail
Washington - TCSS - 342
import java.util.*;import java.io.*;class UseScanner { / This method reads a text file and prints its contents to the screen / it illustrates using the scanner class. / the name of the text file is taken from the input arguments
Washington - TCSS - 342
Reading file Hw1testinput1.txtE 13.32%N 12.1%T 9.49%I 9.19%S 7.96%A 7.35%L 5.82%R 5.67%O 4.13%M 3.52%F 3.37%C 3.22%U 2.6%G 2.14%P 1.99%H 1.84%D 1.68%W 1.07%Y 1.07%X 0.92%V 0.61%B 0.46%J 0.31%K 0.15%Q 0.0%Z 0.0%
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B (Hong) Assignment #7 version 1.1 Due: Thursday, March 9, 2006, 4:15 PM Please show your work and explain your reasoning. Write legibly (or type) and organize your answers. Turn in your
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B Written Assignment 1, Answers 1. Using the definition of logarithm in class, show that logA B = logC B / logC A . Let X = logA B Let Y = logC B Let Z = logC A. To show this, we will sh
Washington - TCSS - 342
University of Washington, Tacoma TCSS 342, Winter 2006, Section B (Hong) Assignment #7 solution version 1.0 Due: Thursday, March 9, 2006, 4:15 PM Please show your work and explain your reasoning. Write legibly (or type) and organize your answers. Tur