Quiz 1,2, and 3
29 terms
seffie99
2.8 syntax errors
11 terms
knives84
Final Part 2
272 terms
Isa_W
Chapter 5 JAVA
64 terms
JohnSingleton
Sets found in the same folderAP Computer Science Unit 4 Test
47 terms
ellerkerrPLUS
APCS Chapter 14 Sorting
110 terms
XxAllenxX2002
chapter 6 part2
52 terms
Christhian_Perez
Java Test 3
51 terms
vcole31
Other sets by this creatorBIO 2 EXAM 3 Craddock
80 terms
mia_dora_fletcher
BIO 2 Quiz 3 Craddock
31 terms
mia_dora_fletcher
Bio Lecture Exam 2 Craddock
60 terms
mia_dora_fletcher
Bio Lecture Quiz 2
45 terms
mia_dora_fletcher
Recommended textbook solutionsService Management: Operations, Strategy, and Information Technology
7th EditionJames Fitzsimmons, Mona Fitzsimmons
103 solutions
Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th EditionDavid A. Patterson, John L. Hennessy
220 solutions
Information Technology Project Management: Providing Measurable Organizational Value
5th EditionJack T. Marchewka
346 solutions
Service Management: Operations, Strategy, and Information Technology
7th EditionJames Fitzsimmons, Mona Fitzsimmons
103 solutions
Other Quizlet setsChapter 2
31 terms
Emalee_Warren
Vector part 1 Ashton Roess 3A
20 terms
ashtonroess8
Comp Sci Midterm
38 terms
kwang3811
Computer Class
12 terms
Natasha_quinones
Related questionsQUESTION
What will this statement do: int item[5] = {2,12,1,2,9,5};
12 answers
QUESTION
Which type of file can be stored on one machine and used by multiple users on other machines?
15 answers
QUESTION
What is a translator and what does it do?
15 answers
QUESTION
Tell Tracker what are the advantages of Java and disk files. over [1], [2], and [3]
2 answers
Nội dung chính Show
- Exception Handling in Java
- What is an Exception?
- Exception categories in Java
- When to use Exceptions
- How do you do exception handling?
- Claiming Exceptions
- Throwing Exceptions
- Catching Exceptions
- What happens if an exception is not caught?
- Instance methods in exception objects
- Deitel code examples in Chapter 13 folder
- Exception Handling in C++
- What happens if an exception does not have a matching catch clause?
- What will happen if an exception is thrown for which no matching catch [] block is defined?
- What happens if an exception is thrown in a try block?
- What will happen if an exception is thrown and there is no catch block to deal with it in Java?
Exception Handling in Java
What is an Exception?
An exception is an object that represents an error or an exceptional situation that has occurred- Could represent the run-time environment detecting an operation that's impossible to carry out
- Could be created by a method in response to a special situation
- In Java, exception objects are all children of class Throwable. This base class has two main child classes:
- class Exception: base class for most exception types
- class Error: class that represents things that are usually not recoverable [e.g. VirtualMachineError]
- Examples of a few common exception types:
- IndexOutOfBoundsException: for when an array index is out of bounds
- FileNotFoundException: when attempting to open a file that does not exist
- Java has many built-in exception classes in its libraries
- Users can create new exception types -- just derive from class Exception or one of its subclasses
- Exceptions are used to build robust programs. Exceptions allow the programmer to recover from an error or exceptional event. Java was originally a language for embedded systems [TVs, phones, watches, etc.] These systems should never stop working, exceptions are needed for these systems.
Exception categories in Java
Java has two primary kinds of exceptions- Checked Excepctions [Normal exceptions]
- These are exceptions that could occur in any program, regardless of how well it is coded
- These are typically thrown manually by a method
- If a checked exception is "thrown" in a method, then the method must catch it, or must "claim" it [i.e. declare that it can be thrown].
- Example: FileNotFoundException. No matter how well a program is coded, a user might pick a non-existent file to open
- Unchecked Exceptions [Runtime exceptions]
- These have the potential to be in any code, arising automatically due to runtime issues
- The class RuntimeException and all of its subclasses are in this category
- Compiler does not require these to be "caught" or "claimed".
- Usually, a runtime exception can be eliminated by coding better
- Example: IndexOutOfBoundsException. This occurs when user of an array uses a bad index. Fix the code so this won't happen!
When to use Exceptions
- Exception handlers shouldn't be used for all traditional error-checking situations. Sometimes conventional error-checking is more appropriate.
- Exceptions are best used when:
- its a problem that occurs less frequently
- the error doesn't need to be [or can't be] handled in the block in which it occurs
- an error would otherwise result in program termination
- A common exception handling situation:
- Every method has a specific job to do. Suppose a method is supposed to return an answer as the return value.
- If a situation could occur so that the method could not arrive at an answer, what should it return? [i.e. there is no valid answer!]
- By throwing an exception, the method can bypass the need for any return -- sending out an error message [via an exception object] instead]
- The calling module [whoever called the function] would have to "catch" the exception, to discover what occurred
How do you do exception handling?
The process involves:
- Claiming exceptions - each method needs to specify what exceptions it expects might occur [i.e. what it will throw, and not handle internally]
- Throwing an exception - When an error situation occurs that fits an exception situation, an exception object is created and thrown
- Catching an exception - Exception handlers [blocks of code] are created to handle the different expected exception types. The appropriate handler catches the thrown exception and performs the code in the block
Claiming Exceptions
In a Java method claim an exception with the keyword throws. This goes at the end of the method's prototype and before the definition. Multiple exceptions can be claimed, with a comma-separated list. Examples: public void myMethod[] throws IOException public void yourMethod[] throws IOException, AWTException, BobExceptionThrowing Exceptions
In Java, use the keyword throw, along with the type of exception being thrown. An exception is an object, so it must be created with the new operator. It can be created within the throw statement or before it. Examples: throw new BadHairDayException[]; MyException m = new MyException[]; throw m; if [personOnPhone != bubba] throw new Exception["Stranger on the phone!!"]; Note that this is different than the keyword throws, which is used in claiming exceptions.Catching Exceptions
Keywords: try, catch, finally- Any group of statements that might throw an exception [or where you want to watch for Runtime or Error exceptions] -- place inside a try block.
- If an exception occurs [i.e. "is thrown"], execution ends at the "throw point" and will only resume if the exception is "caught"
- After the try block, there should be one or more catch blocks
- Each catch block has a parameter -- the type of exception that this block will handle
- There can be multiple catch blocks
- If an exception is thrown, the first matching catch block is the one that runs
- An optional finally block can be used
- A finally block is always executed, no matter how control leaves the try block
Trivial Example
- Look at this example. What happens if we enter 0 for y?
- Now try this revised version of the example. When run, it does not crash when the exception is thrown. Instead, the exception is handled.
- Is this really a place where we need exception handling techniques, though?
Example Layout -- try, catch, finally
try { // lots of IO code opening and reading from/to files } catch [FileNotFoundException] { // tell the user and probably repeat try block } catch [EOFException] { // hit the End Of File marker early } catch [IOException] { // blanket catch for all other IO problems } finally { // make sure to close any files that might be open }What happens if an exception is not caught?
- If an exception is not caught [with a catch block], the runtime system will abort the program [i.e. crash] and an exception message will print to the console. The message typically includes:
- name of exception type
- short description
- stack trace
- For a
checked exception, a method must either catch an exception [handle internally] or claim it [declare that it will be thrown]
- This is enforced by compiler.
- Claiming an exception is giving information to the caller, so that they will know to catch it!
- For an unchecked exception [runtime exception], there's no compiler enforcement.
- Runtime exceptions could occur anywhere, arising automatically.
- Usually can be fixed by better coding
- Examples: Division by zero, array index out of bounds, null pointer exception
Instance methods in exception objects
Exception objects are created from classes, which can have instance methods. There are some special instance methods that all exception objects have [inherited from Throwable]:- public String getMessage[] -- returns a detailed message about the exception
- public String toString[] -- returns a short message describing the exception
- public String getLocalizedMessage[]
- public void printStackTrace[]
Deitel code examples in Chapter 13 folder
Exception Handling in C++
While the concepts are largely the same, the syntax differs a little- In C++, there's no finally block
- In C++, throw is used for both "claiming" and throwing exceptions
- C++ uses keywords try, catch, throw
- Class exception comes from the library and is part of namespace std #include using std::exception;
- There are some other libraries for dealing with other kinds of exceptions. For example, runtime errors: #include using std::runtime_error;
- In C++, the "claiming" of an exception [specifying a throw list] on the function prototype looks like this: void someFunction[] throw [DivideByZero, SomeOtherException];
- A few code examples from Deitel C++:
- Illustration of a custom DivideByZero exception class
- General exception usage [try, catch, throw]
- Stack unwinding. Also illustrates functions claiming exceptions
What happens if an exception does not have a matching catch clause?
If an exception does not have a matching catch clause, the current method terminates and throws the exception to the next higher level. If there is no matching catch clause at any higher level, then the program terminates with an error. Your program can do anything with the exception object that it chooses to.
What will happen if an exception is thrown for which no matching catch [] block is defined?
If there is no catch block at the current scope matching the thrown exception, the current scope is exited, and all automatic [local nonstatic] objects defined in that scope are destroyed. The surrounding scope [which might be function scope] is checked for a matching handler.
What happens if an exception is thrown in a try block?
If an exception is thrown inside the try-block, for instance from the divide method, the program flow of the calling method, callDivide, is interrupted just like the program flow inside divide. The program flow resumes at a catch-block in the call stack that can catch the thrown exception.
What will happen if an exception is thrown and there is no catch block to deal with it in Java?
if you don't handle exceptions When an exception occurred, if you don't handle it, the program terminates abruptly and the code past the line that caused the exception will not get executed.