O & error-handling
[Pages:36]File I/O & error-handling
Crash course
Ensuring reliability
? We do our best to ensure program correctness through a rigorous testing and debugging process
? To ensure reliability, we must anticipate conditions that could cause problems, and try to deal with them before the problems occur
? In Java, we have exception handling, a powerful tool for ensuring program reliability
Exceptions
? An error condition that occurs during program runtime is called an exception
? Exceptions are represented by exception objects, which are generated (thrown) in response to error conditions
? Java includes a rich set of routines for dealing with such circumstances: this is known as exception handling
Catching exceptions: try/catch block
? Consists of try block followed by one or more catch blocks
? try block: encloses code that might throw an exception
? catch block(s) deal with any exception(s) thrown
Syntax for try/catch block
try { // code that may throw an exception
} catch (ExceptionType parameterName) { // code that handles an exception thrown // in the try block
}
Catching Exceptions
? Statements in the try block are executed in sequence.
? If no error occurs then no exception is thrown, all statements in the try block are executed and the catch block(s) will be skipped
Catching Exceptions
? When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed
? Provided the catch block does not halt the program or return from the method, program execution continues at the first statement following the catch block
? If more than one exception is possible, multiple catch blocks can be written
Example
int value; // number to be supplied by user String input; // value read from keyboard Scanner kb = new Scanner (System.in);
System.out.print ("Enter a number: "); input = kb.nextLine();
try { value = Integer.parseInt(input);
} catch (NumberFormatException e) { System.out.println (input + "is not valid.\n" + "Please enter digits only.");
}
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java programming 1 introduction to java and o reilly
- o error handling
- java file i o city university of new york
- java files copyright © tutorialspoint
- streams file i o and networking
- cse 2231 java input output
- writing to a text file java i o and exceptions
- the file class binary i o and serialization
- i o fundamentals usf computer science
- java files and i o
Related searches
- cash handling procedures template
- cash handling policy and procedures
- data classification and handling policy
- basic cash handling procedures
- cash handling procedures best practices
- quality material handling inc
- restaurant cash handling policy template
- warehouse material handling procedures
- material handling procedures samples
- manual materials handling procedures
- cash handling procedure manual
- sample material handling policy