Download Föreläsningsserien TTIT71 Programmering Fö7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Föreläsningsserien
TTIT71 Programmering Fö7
John Wilander, Linköpings
universitet
Dagens föreläsning
• Undantag
1.
2.
3.
4.
5.
6.
7.
8.
9.
Intro och översikt objektorientering
Klasser och objekt
Inkapsling och arv
Åsidosättning och överlagring
Abstrakta klasser och interface
Collections, generics, uppräkningar
Undantag, design, dokumentation
Extraföreläsning
Resurstillfälle: Genomgång gammal tenta
Undantag – Exception
• Fel uppstår under körning
– Checked och Unchecked
• Design
– Identifiera klasser
• Dokumentation
– JavaDoc
– Klassdiagram
• Metoder kan …
– Kasta undantag – throw
– Fånga undantag – catch
Undantag är objekt
Undantag är objekt
import java.text.ParseException;
import java.text.ParseException;
public void checkMathExpression(String input)
throws ParseException {
int index = 0;
char ch = input.charAt(index);
while(index < (input.length() – 1) {
ch = input.charAt(index++);
if(!isOperator(ch) && !isDigit(ch) &&
!isParanthesis(ch)) {
throw new ParseException(”Disallowed character”,
index);
}
}
}
public void checkMathExpression(String input)
throws ParseException {
int index = 0;
char ch = input.charAt(index);
while(index < (input.length() – 1) {
ch = input.charAt(index++);
if(!isOperator(ch) && !isDigit(ch) &&
!isParanthesis(ch)) {
throw new ParseException(”Disallowed character”,
index);
}
}
}
Arvshierarki för undantag
Arvshierarki för undantag
Throwable
Throwable
Exception
Exception
ArrayIndexOutOfBounds …
RuntimeException
RuntimeException
ArithmeticException
IOException
IOException
ClassCastException
InterruptedException
InterruptedException
TooManyListenersException
TooManyListenersException
Arvshierarki för undantag
Throwable
NoClassDefFoundError
Error
Unchecked exception
Throwable
IllegalAccessError
Exception
ArrayIndexOutOfBounds …
RuntimeException
ArithmeticException
IOException
ClassCastException
Exception
RuntimeException
ArrayIndexOutOfBounds …
ArithmeticException
ClassCastException
InterruptedException
TooManyListenersException
Checked exceptions
Throwable
Exception
IOException
InterruptedException
TooManyListenersException
Välja typ av undantag
• Checked exception
– När undantaget förväntas uppstå
– När det går att rädda systemet
• Unchecked exception
– När undantaget inte förväntas uppstå
– ”Something went horribly wrong!”
Egna undantag
public class NoSuchStudentException
extends Exception {
Egna undantag
public class NoSuchStudentException
extends RuntimeException {
public NoSuchStudentException(String message) {
super(message);
}
}
public NoSuchStudentException(String message) {
super(message);
}
}
!
"
Kasta vidare undantag
• En kedja av metoder kan ha throws i sin
signatur
• Lämnar över ansvaret att fånga högre upp
i anropskedjan
!
"
Stack Trace
Välkommen
Ange ett index: 2
Namn: Svenne
Ange ett index: hej
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at scratch.ExceptionTest.readInt(ExceptionTest.java:20)
at scratch.ExceptionTest.readAndPrint(ExceptionTest.java:15)
at scratch.ExceptionTest.main(ExceptionTest.java:11)
java.lang.Throwable.printStackTrace()
Stack Trace
StudentDatabase
Dagens föreläsning
• Undantag
– Checked och Unchecked
throw
main()
• Design
Student
– Identifiera klasser
throw
• Dokumentation
method()
ITare
– JavaDoc
– Klassdiagram
method()
Dagens föreläsning
• Undantag
– Checked och Unchecked
JavaDoc – koddukumentation
• JavaDoc standardiserat sätt att
dokumentera programkod
• Design
– Identifiera klasser
• Dokumentation
– JavaDoc
– Klassdiagram
• Används för att generera HTMLdokumentation i stil med API:et
JavaDoc – koddukumentation
/**
* Checks if a <code>Word</code> is correctly spelled
* @param word the <code>Word</code> to check
* @return <code>true</code> if the <code>Word</code> is
* correctly spelled
*/
public boolean isCorrectlySpelled(Word word) {
return listDictionary.contains(word);
}
/**
* Adds a new <code>Word</code> to this dictionary
* @param newWord the <code>Word</code> to add
*/
public void addWord(Word newWord) {
listDictionary.add(newWord);
Collections.sort(listDictionary);
}
JavaDoc – koddukumentation