Upload
phuongsky
View
236
Download
0
Embed Size (px)
Citation preview
8/10/2019 Lect3 Exceptions
1/39
Software Engineering
2013
Lecture 3:Exception
8/10/2019 Lect3 Exceptions
2/39
2013 FIT330 Software Engineering 2
Outline
Why exception?
Java error handling mechanism
checked & unchecked exceptions
Working with exceptions
esign issues
8/10/2019 Lect3 Exceptions
3/39
2013 FIT330 Software Engineering 3
Why exception?
Problem: how to design robustprocedures i!e! "ehave in a well#de$ined way when errors
occur
%euirements: not to terminate a"ruptly
provides an approximationo$ the normal "ehaviour
1
8/10/2019 Lect3 Exceptions
4/39
2013 FIT330 Software Engineering 4
Example: invalid input pro"lem
' "ehaviour is reuired $or invalid input:/*** @effects* if a is sorted in ascending order* returns true
* else returns false*/publicstaticbooleansorted(int[] a) { intprev = a[0];for(inti = 1; i < a.lengt; i!!) {
if(prev
8/10/2019 Lect3 Exceptions
5/39
2013 FIT330 Software Engineering 5
(wo common solutions
%eturns a )special) value: error is treated part o$ the normal $low
*enerates an special )signal):
error is an exceptional case +separate $rom thenormal $low,
?Which solution is better
8/10/2019 Lect3 Exceptions
6/39
2013 FIT330 Software Engineering 6
-olution . example
publicstaticbooleansorted(int[] a) {
if(a == null) returnfalse;
if(a.lengt
8/10/2019 Lect3 Exceptions
7/392013 FIT330 Software Engineering 7
-olution .)s limitations
/o valid special values $or total procedures 0alue may "e ignored "y caller
'n additional check is always reuired
%eturned value does not carry error details
8/10/2019 Lect3 Exceptions
8/392013 FIT330 Software Engineering 8
-olution 1: Exception
(he special signal is called exception no returned values needed
2aller can "e $orced to handle exceptions
2aller may deligate the handling Exception is a data type that carry error details
8/10/2019 Lect3 Exceptions
9/392013 FIT330 Software Engineering 9
-olution 1 example
publicstaticbooleansorted(int[] a)throws#ull$ointer%&ception { if(a == null)
thrownew#ull$ointer%&ception('sorted arra is null');if(a.lengt
8/10/2019 Lect3 Exceptions
10/392013 FIT330 Software Engineering 10
Java error handling mechanism
se special )signal)
Execution errors include:
environmental errors: derived $rom %rrorclass
program errors: exceptions
/ew exception types can "e created:
checked or unchecked
4ethods are speci$ied with exceptions
2
8/10/2019 Lect3 Exceptions
11/392013 FIT330 Software Engineering 11
Java exception type hierarchy
(hrowa"le
Error Exception
%untimeException +checked exceptions,
+unchecked exceptions,
8/10/2019 Lect3 Exceptions
12/392013 FIT330 Software Engineering 12
2hecked vs! unchecked exception
2hecked exceptions: must "e handled
derived $rom %&ception
nchecked exceptions: need not "e handled +automatically handled "y
Java run#time,
used $or code sa$ety
su"types o$ unti+e%&ception
8/10/2019 Lect3 Exceptions
13/392013 FIT330 Software Engineering 13
Working with exceptions
etermine the exception type 2reate a new exception type
-peci$y a procedure with exceptions
-peci$y a type with exceptions
(hrow an exception in the code
5andle an exception
3
8/10/2019 Lect3 Exceptions
14/392013 FIT330 Software Engineering 14
etermine the exception type
(ypes o$ error: invalid input: e!g! array is null
logic: e!g! division "y 6ero
others: e!g! disk access $ailure
Exception depends on the )expectedness) o$the error:
+highly, expected: use checked exception
+mildly, unexpected: use unchecked exception
%euse exceptions where possi"le
8/10/2019 Lect3 Exceptions
15/392013 FIT330 Software Engineering 15
5ow to name an exception?
Exception name 7 type o$ error 8 9Exception
Examples:
array is null ; #ull$ointer%&ception
division "y 6ero ; ,ivide-ero%&ception
disk access $ailure ; ,isccess%&ception
8/10/2019 Lect3 Exceptions
16/392013 FIT330 Software Engineering 16
2reate a new exception
2reate a su"type o$ %&ception+i$ checked,or unti+e%&ception+i$ unchecked,
8/10/2019 Lect3 Exceptions
17/39
2013 FIT330 Software Engineering 17
Example
public class #on$ositive%&ception
extendsunti+e%&ception {
public%+pt%&ception(tring +sg) {
super(+sg);
"
"
8/10/2019 Lect3 Exceptions
18/39
8/10/2019 Lect3 Exceptions
19/39
2013 FIT330 Software Engineering 19
Example: fact
/*** @effects* if n is non2positive3* tro4s #on$ositive%&ception* else* returns te factorial of n
*/publicstaticintfact(intn) throws#on$ositive%&ception
8/10/2019 Lect3 Exceptions
20/39
2013 FIT330 Software Engineering 20
searc
/*** @requires ais sorted* @effects * if a is null* tro4s #ull$ointer%&ception
* else if & is not in a* tro4s #ot5ound%&ception* else* returns i suc tat a[i] = &.*/publicstaticintsearc(int[] a3 int&)
throws#ull$ointer%&ception3 #ot5ound%&ception
8/10/2019 Lect3 Exceptions
21/39
8/10/2019 Lect3 Exceptions
22/39
2013 FIT330 Software Engineering 22
Example: 6eicle
c7.veicle&.6eicle /ote:
6eiclethrows #ot$ossi8le%&ceptionin
constructor and setters 6eicleimplements 9o+para8leand provides
an implementation $or co+pare:o:
co+pare-#a+e: throws two run#time exceptions
8/10/2019 Lect3 Exceptions
23/39
2013 FIT330 Software Engineering 23
(hrow an exception
se the tro4statement Example:
/**
* (o+itted)
*/
publicstaticintfact(intn) throws#on$ositive%&ception {
if(n < 0)
thrownew#on$ositive%&ception( 'fact() input is not positive '! n);
returnc.#u+.fact(n);
"
8/10/2019 Lect3 Exceptions
24/39
2013 FIT330 Software Engineering 24
5andle an exception
-peci$y in tro4sor handle in the code -urrounds the using code with tr!!!catc
(hree techniues applied to the catc"lock:
Logging
4asking
%e$lecting
' com"ination o$ the a"ove techniues is alsocommon
8/10/2019 Lect3 Exceptions
25/39
2013 FIT330 Software Engineering 25
Example
int n = 10;try{
intf = fact(n);
ste+.out
.println('fact('!n!') '! f);" catch(#on$ositive%&ception e) {
// andle e&ception
"
8/10/2019 Lect3 Exceptions
26/39
2013 FIT330 Software Engineering 26
Log an exception
%ecord the exception details +e!g! to $ile, Example:
int n = 10;
try{
intf = fact(n);
ste+.out.println('fact('!n!') '! f);
" catch(#on$ositive%&ception e) {
// sould not appen3 log
ste+.err.println('%rror invalid input '!e.getessage());
// +ore details
// e.printtac:race();
"
8/10/2019 Lect3 Exceptions
27/39
2013 FIT330 Software Engineering 27
4ask an exception
When exceptions are expected as part o$ thelogic
5andles the exception to terminate normally
+i$ reuired, return an exceptional value O$ten used together with logging
8/10/2019 Lect3 Exceptions
28/39
2013 FIT330 Software Engineering 28
Example
/*** @effects* if n is non2positive* log error and return 21* else* return te factorial of n
*/publicstaticintco+pute5act1(intn) { try{ intf = fact(n); returnf; " catch(#on$ositive%&ception e) {
// log ste+.err.println('%rror invalid input '!
e.getessage()); // +as using 21 return21; "
"
8/10/2019 Lect3 Exceptions
29/39
2013 FIT330 Software Engineering 29
%e$lect an exception
8/10/2019 Lect3 Exceptions
30/39
2013 FIT330 Software Engineering 30
%e$lection example
/*** @effects* if n is non2positive* tro4 #ot$ossi8le%&ception* else* return te factorial of n*/publicstaticvoidco+pute5act(intn)
throws#ot$ossi8le%&ception { try{ intf = fact(n);
ste+.out.println('fact('!n!') '! f); " catch(#on$ositive%&ception e) { thrownew#ot$ossi8le%&ception( '9ould not co+pute fact('!n!')'); "
"
8/10/2019 Lect3 Exceptions
31/39
2013 FIT330 Software Engineering 31
esign issues
4ethod overriding with exceptions %euse exceptions where possi"le
2onsider the context o$ use:
partial procedures may use special values
total procedures need exceptions to de$ine"ehaviour $or all inputs
ont overuse exceptions: excessive exception handling degrades
per$ormance
4
8/10/2019 Lect3 Exceptions
32/39
2013 FIT330 Software Engineering 32
Overriding: header rule +review,
=nherited methods must "e header compatible 4ethod header includes:
signature: method name num"er and types o$parameters
return type
thrown exceptions +i$ any,
2ompati"ility means:
same signature
return type: same +Jdk @ .!A, or su"type +B7 .!C,
exceptions: +., su"set +1, new su"types or
unchecked
8/10/2019 Lect3 Exceptions
33/39
2013 FIT330 Software Engineering 33
Example: 6eicle(5
c7.veicle&.[-us39ar] /ote:
-us& 9arconstructors now throw
#ot$ossi8le%&ception -uch exceptions may "e omitted
(hese constructors may throw additional run#timeexceptions
?
8/10/2019 Lect3 Exceptions
34/39
2013 FIT330 Software Engineering 34
8/10/2019 Lect3 Exceptions
35/39
2013 FIT330 Software Engineering 35
(otal procedure
/** * @effects* if a is null* tro4s #ull$ointer%&ception* else if a is sorted in asc order
* return true* else return false
*/ publicstaticbooleansorted(int[] a)
throws#ull$ointer%&ception
8/10/2019 Lect3 Exceptions
36/39
2013 FIT330 Software Engineering 36
Overuse example
/*** @requiresa = null
* @effects
*
* ifa is sorted in ascending order* return true
* else
* return false
*/booleansorted(int[] a)
8/10/2019 Lect3 Exceptions
37/39
2013 FIT330 Software Engineering 37
Which part is overusing?
booleansorted(int[] a) { intprev; try{ prev = a[0]; " catch(nde&?ut?f-ounds%&ception e) { returntrue;
"
for(inti = 1; i < a.lengt; i!!) { if(prev
8/10/2019 Lect3 Exceptions
38/39
8/10/2019 Lect3 Exceptions
39/39
uestions?