Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been...
-
Upload
brianne-rogers -
Category
Documents
-
view
215 -
download
1
Transcript of Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been...
![Page 1: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/1.jpg)
ExceptionsHandling the unexpected
![Page 2: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/2.jpg)
RHS – SWC 2
The Real World
• So far, most of our code has been somewhat näive
• We have assumed that nothing goes wrong…– User enters correct input– We never address outside the
boundaries of an array– …and so on
![Page 3: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/3.jpg)
RHS – SWC 3
The Real World
• Of course, the real world works differently
public class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
// Now what?
}
...
}
![Page 4: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/4.jpg)
RHS – SWC 4
The Real World
• Possible actions depend on our interpre-tation of this situation– Legal – just do business logic– Illegal, and we know what to do – perhaps just
do nothing– Illegal, and we do not know what to do!
• Error detection and error handling are often separated in code!
![Page 5: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/5.jpg)
RHS – SWC 5
The Real World
• Problem: Can become very complex to ”drag” error handling code around in code for business logic
• Error-handling may be application-specific– GUI-application: Pop-up window– Other: Write to a log file
• Business logic code should not choose strategy for error handling!
![Page 6: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/6.jpg)
RHS – SWC 6
The Real World
• Next problem: Error handling code is often very ”far away” from error detction
main askUser doAction makeWithdraw withdraw
Error detected
Error handling
?
![Page 7: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/7.jpg)
RHS – SWC 7
Exceptions
• The mechanism for crossing the gap of method calls is exceptions– An exception in itself is ”just another class”– We can create exception objects just as we
can create other objects– An exception object contains information
about the type of error which occurred– Java contains several built-in exception
classes, forming an inheritance hierarchy
![Page 8: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/8.jpg)
RHS – SWC 8
Throwing and catching
• Exception can be thrown and catched
• What does that mean!?
• A very different flow than usual method calls
• An exception is thrown up through the chain of method calls
![Page 9: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/9.jpg)
RHS – SWC 9
Throwing and catching
main askUser doAction makeWithdraw withdraw
Error detected – throw!
Who will catch the exception?
![Page 10: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/10.jpg)
RHS – SWC 10
Throwing and catching
• A throw can look like this in Java:
public void withdraw(double amount)
{
if (amount > balance)
{
IllegalArgumentException ex =
new IllegalArgumentException(”...”);
throw ex;
}
balance = balance – amount;
}
NOTE!
![Page 11: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/11.jpg)
RHS – SWC 11
Throwing and catching
• An exception is now thrown; this changes the flow of code immediately!
• Remaining code in the method throwing the exception is not executed
• Somebody must catch the exception
• In order to catch the exception, we must write an exception handler
![Page 12: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/12.jpg)
RHS – SWC 12
Throwing and catching
• General exception handler structure:
try
{
// Code which may throw an exception
...
}
catch (ExceptionType ex)
{
// Proper handling of exception
...
}
![Page 13: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/13.jpg)
RHS – SWC 13
Throwing and catching
try
{
myAccount.withdraw(1000);
myAccount.getTransactions();
...
}
catch (IllegalArgumentException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
![Page 14: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/14.jpg)
RHS – SWC 14
Throwing and catching
• Things to note:– Error detection (throw) and error handling
(try/catch) is usually not in the same method– The catch statement only catches exceptions
of the specified type– Information about the error is found implicitly
– by the type of the exception – and explicitly from e.g the text stored in the object
![Page 15: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/15.jpg)
RHS – SWC 15
Throwing and catching
• Throw early, catch late!
• If you cannot fix a pro-blem correctly, throw an exception
• Only catch an exception if you really know how to fix the problem
![Page 16: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/16.jpg)
RHS – SWC 16
Throwing and catching
try
{
myAccount.withdraw(1000);
myAccount.getTransactions();
...
}
catch (Exception ex)
{
// do nothing...
}
Tempting, but bad…!
![Page 17: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/17.jpg)
RHS – SWC 17
The finally Clause
• Sometimes we need to execute some specific code after an exception occurs
• Typically ”clean-up” code – close a file connection, a database connection, etc.
• Where do we put this code…?– In exception handlers? Difficult, who actually
catches the exception…– In a finally clause!
![Page 18: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/18.jpg)
RHS – SWC 18
The finally Clause
PrintWriter out = new PrintWriter(filename);...
try{
writeData(out);}finally{
// This code will always be executed,// even if the above code throws an exceptionout.close();
}
![Page 19: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/19.jpg)
RHS – SWC 19
The finally Clause
• The code in the finally clause is guaran-teed to be executed, in one of these ways:– After completing the last statement in the try
block– After completing the last statement in a catch clause, if an exception is caught
– When an exception was throw in the try block and not caught
![Page 20: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/20.jpg)
RHS – SWC 20
The finally Clausetry{}catch{}finally{}
try{try{}finally{}
}catch{}
GOODNOT SOGOOD
![Page 21: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/21.jpg)
RHS – SWC 21
Checked and Unchecked
• How do I know what exceptions some piece of code can throw…?
• Difficult to code a method correctly without this knowledge
• Two types of exceptions exist– Checked exception– Unchecked exception
![Page 22: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/22.jpg)
RHS – SWC 22
Checked and Unchecked
• Checked exception– Used for problems beyond the control of the
programmer– Corrupted file, network problems, etc..– Compiler insists that you explicitly decide
what to do about it• Option 1: Re-throw the exception• Option 2: Handle the exception, using a catch
clause matching the exception
![Page 23: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/23.jpg)
RHS – SWC 23
Checked and Unchecked
// Suppose draw() can throw DrawExceptiondraw(Figure f);
// Compiler will not like this!public void drawOne(Figure f){getScreen().draw(f);
}
![Page 24: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/24.jpg)
RHS – SWC 24
Checked and Unchecked
// Option 1: Re-throw the exception// (i.e. do nothing…)
public void drawOne(Figure f) throws DrawException{getScreen().draw(f);
}
![Page 25: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/25.jpg)
RHS – SWC 25
Checked and Unchecked
// Option 2: Handle the exception
public void drawOne(Figure f){
try{ getScreen().draw(f);}catch (DrawException de){
// Code for handling the problem}
}
![Page 26: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/26.jpg)
RHS – SWC 26
Checked and Unchecked
drawdrawOneOption 1: drawOne does nothing, so it must annonce that it will (re)throw the exception
drawdrawOneOption 2: drawOne handles the exception, so it is ”consumed” by drawOne
![Page 27: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/27.jpg)
RHS – SWC 27
Checked and Unchecked
• Unchecked exception– Used for problems which the programmer
should be able to prevent– Null reference, out of bounds reference,…– Why do we have these…? Accidents do
happen…!– Unchecked exceptions are not announced
![Page 28: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/28.jpg)
RHS – SWC 28
Making your own exceptions
• Throw exceptions that are as specific as possible – also in terms of type
• Many built-in exceptions to choose from
• Can be appropriate to create your own exceptions
• Just extend existing class
![Page 29: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/29.jpg)
RHS – SWC 29
Making your own exceptions
public class InsufficientFundsExceptionextends RunTimeException
{public InsufficientFundsException() {}
public InsufficientFundsException(String message){ super(message);}
}
![Page 30: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/30.jpg)
RHS – SWC 30
Exceptions vs. Flow control
• Exceptions change the linear flow of code, just like if, while, etc.
• However, they are only intended for error detection and handling
• Do not use exceptions as a substitute for ordinary flow control
![Page 31: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/31.jpg)
RHS – SWC 31
Exceptions vs. Flow control
for (int i = 0; i < noOfElements; i++)
myArray[i] = i;
try {
for (int i = 0; /* No Test?? */ ; i++) myArray[i] = i;
}catch (ArrayIndexOutOfBoundsException e)
{}
GOOD
BAD
![Page 32: Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…](https://reader035.fdocuments.us/reader035/viewer/2022062719/56649ecf5503460f94bdd5f9/html5/thumbnails/32.jpg)
RHS – SWC 32
Exceptions summary
• Throw early, catch late
• Only catch, if you can handle the problem correctly
• You must deal with checked exceptions
• Use try, catch and finally appropriately
• Make your own exception classes, if you really need them
• Exceptions are not for flow control