Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software...
Transcript of Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software...
![Page 1: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/1.jpg)
Software Engineering I (02161)Week 1
Assoc. Prof. Hubert Baumeister
DTU ComputeTechnical University of Denmark
Spring 2013
![Page 2: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/2.jpg)
Contents
Course Introduction
Introduction to Software Engineering
Practical Information
Eclipse, JUnit, and Exercises
User-defined Exceptions
![Page 3: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/3.jpg)
The course
I 5 ECTS course 02161: Software Engineering 1I Target group: Bachelor in Software Technology and IT and
Communication in the second semesterI Learning objectives
I To have an overview over the field software engineeringand what is required in software engineering besidesprogramming
I To be able to take part in bigger software developmentprojects
I To be able to communicate with other software designersabout requirements, architecture, design
→ To be able to conduct a smaller project from an informaland open description of the problem
![Page 4: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/4.jpg)
Who are we?
I 117 students with different backgroundsI Bachelor Softwaretek.: 60I Bachelor It og Kom.: 44I Other bachelor: 9I Other: 4
I TeacherI Hubert Baumeister, Assoc. Prof. at DTU Informatik
([email protected]; office 322.010 (will be changing duringthe course :-( )
I 3 Teaching assistantsI Thomas FeldI Patrik ReppienI NN
![Page 5: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/5.jpg)
Contents
Course Introduction
Introduction to Software EngineeringIntroductionDevelopment Example
Practical Information
Eclipse, JUnit, and Exercises
User-defined Exceptions
![Page 6: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/6.jpg)
Building software
Tools and techniques for building software, in particular largesoftware
![Page 7: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/7.jpg)
What is software?
I Software is everywhereI Stand-alone application (e.g. Word, Excel)I Interactive transaction-based applications (e.g. flight
booking)I Embedded control systems (e.g., control software the
Metro, mobile phones)I Batch processing systems (e.g. salary payment systems,
tax systems)I Entertainment systems (e.g. Games)I System for modelling and simulation (e.g. weather
forecasts)I Data collection and analysing software (e.g. physical data
collection via sensors, but also data-mining Googlesearches)
I System of systems (e.g. cloud, system of interactingsoftware systems)
I . . .
![Page 8: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/8.jpg)
What is software?
I Software: Not only the computer program(s) but alsoI Documentation (User–, System–)I Configuration files, . . .
I Types of softwareI Mass production: The maker of the software owns the
system specificationI Customised software: The customer owns the system
specificationI Mixture: Customised software based on mass production
software→ Not one tool, method, or theory
I Though there are general principles applicable to alldomains
![Page 9: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/9.jpg)
Attributes of Software
I MaintainabilityI Dependability and securityI EfficiencyI Acceptability
![Page 10: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/10.jpg)
Software Engineering
Software Engineering Definition (Sommerville 2010)Software engineering is an engineering discipline that isconcerned with all aspects of software production from theearly stages of system specification through to maintaining thesystem after it has gone into use.
![Page 11: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/11.jpg)
Basic Activities in Software Development
I Understand and document what kind of the software thecustomer wants
I Determine how the software is to be builtI Build the softwareI Document and being able to talk about the softwareI Validate that the software solves the customers problem
![Page 12: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/12.jpg)
Example Vending Machine
Design and implement a controlsoftware for a vending machine
![Page 13: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/13.jpg)
Vending Machine: Requirements documentation
I Understand and document what kind of the software thecustomer wants→ Glossary→ Use case diagram→ Detailed use case
![Page 14: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/14.jpg)
GlossaryI Vending machine: The vending machine allows users to
buy fruit.I User: The user of the vending machine buys fruit by
inserting coins into the machine.I Owner: The owner owns the vending machine. He is
required to refill the machine and can remove the moneyfrom the machine.
I Display: The display shows how much money the user hasinserted.
I Buy fruit: Buy fruit is the process, by which the user inputscoins into the vending machine and selects a fruit bypressing a button. If enough coins have been provided theselected fruit is dispensed.
I Cancel: The user can cancel the process by pressing thebutton cancel. In this case the coins he has inserted will bereturned.. . .
![Page 15: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/15.jpg)
Use case diagram
VendingMachine
User
Owner
Buy Fruit
Cancel
Refill Machine
Takeout Money
![Page 16: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/16.jpg)
Detailed Use Case: Buy Fruit
name: Buy fruitdescription: Entering coins and buying a fruitactor: usermain scenario:
1. Input coins until the price for the fruit to be selected isreached
2. Select a fruit3. Vending machine dispenses fruit
alternative scenarios:a1. User inputs more coins than necessarya2. select a fruita3. Vending machine dispenses fruita4. Vending machine returns excessive coins
. . .
![Page 17: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/17.jpg)
Vending Machine: Specify success criteria
I Prepare for the validation→ Create tests together with the customer that show when
system fulfils the customers requirements→ Acceptance testsI Test driven development
→ create tests before the implementationI Otherwise: after the implementation
![Page 18: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/18.jpg)
Functional Test for Buy Fruit Use Case: JUnit Tests
@Testpublic void testBuyFruitExactMoney() {
VendingMachine m = new VendingMachine(10, 10);m.input(1);m.input(2);assertEquals(3, m.getCurrentMoney());m.selectFruit(Fruit.APPLE);assertEquals(Fruit.APPLE, m.getDispensedItem());
}
@Testpublic void testBuyFruitOverpaid() {
VendingMachine m = new VendingMachine(10, 10);m.input(5);assertEquals(5, m.getCurrentMoney());m.selectFruit(Fruit.APPLE);assertEquals(Fruit.APPLE, m.getDispensedItem());assertEquals(2, m.getRest());
}
// more tests// at least one for each main/alternative scenario
![Page 19: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/19.jpg)
Vending Machine: Design and implementation
I Determine how the software is to be built→ Class diagrams to show the structure of the system→ State machines to show how the system behaves
I Build the software→ Implement the state machine using the state design pattern
![Page 20: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/20.jpg)
High-level Class diagram
«enumeration»Fruit
APPLEBANANA
VendingMachinedispensedItem: FruitcurrentMoney: inttotalMoney: intrestMoney: intinput(money: int)select(f: fruit)cancel()
*
![Page 21: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/21.jpg)
Application logic as state machine
![Page 22: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/22.jpg)
Design of the system as class diagram
Uses the state design pattern
«enumeration»Fruit
APPLEBANANA
VendingMachinedispensedItem: FruitcurrentMoney: inttotalMoney: intrestMoney: intinput(money: int)select(f: fruit)cancel()~setIdleState()~dispense(f: Fruit)~setCurrentStateForFruit(f: Fruit)~hasFruit(f: Fruit)
1
«interface»VendingMachineState
input(m: VendingMachine, money: int)select(m: VendingMachinef: fruit)cancel(m: VendingMachine)
IdleState
input(m: VendingMachine, money: int)select(m: VendingMachinef: fruit)cancel(m: VendingMachine)
FruitSelectionState
input(m: VendingMachine, money: int)select(m: VendingMachinef: fruit)cancel(m: VendingMachine)
1
*
m.setCurrentMoney(m.getCurrentMoney() + i);
if (!m.hasFruit(fruit)) { m.setIdleState(); return;}if (m.hasEnoughMoneyFor(fruit)) { m.setIdleState(); m.dispense(fruit);} e l se { m.setCurrentStateForFruit(fruit);}
m.dispense(null);
super.input(m, i);if (m.hasEnoughMoneyFor(selectedFruit)) { m.setIdleState(); m.dispense(selectedFruit);}
m.setIdleState();super.cancel(m);
![Page 23: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/23.jpg)
Vending Machine: Visualization of the Execution
I Documentation of how the implementation of the VendingMachine works:→ Use Interaction Diagrams, aka. Sequence Diagrams
![Page 24: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/24.jpg)
Interaction Diagram: Swing GUI
sd:buy apple
![Page 25: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/25.jpg)
Contents
Course Introduction
Introduction to Software Engineering
Practical Information
Eclipse, JUnit, and Exercises
User-defined Exceptions
![Page 26: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/26.jpg)
Course content
0. Introduction1. Requirements Engineering2. Software Testing (JUnit, Test Driven Development,
Systematic Tests, Code Coverage)3. System Modelling (mainly based on UML)4. Architecture (e.g layered architecture)5. Design (among others Design Patterns and Design by
Contract)6. Software Development Process (focus on agile processes)7. Project Management (project planning)
![Page 27: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/27.jpg)
Approach to teaching
I Providing a general overview of what makes up softwareengineering
I Teach a concrete method of doing a project (i.e. agilesoftware development with test-driven development)
I e.g. test driven development, user stories, agile projectplanning, . . .
![Page 28: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/28.jpg)
Course activities
I Reading assignments before the lecture: I will assume thatyou have read the assignments!!!
I Pre-flight tests checking that you have read theassignments
I Lectures every Monday 13:00 — approx 15:00 (Lectureplan is on the course Web page)
I Exercises in the E-databar (341.003, 015)I Teaching assistants will be present : 15:00 — 17:00I Expected work at home: 5 hours (lecture preparation;
exercises, . . . )I Assignments not mandatory
I But hand-in recommended to get feedbackI Preparation for the examination project
![Page 29: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/29.jpg)
Examination
I Exam project in groups (2—4)I Software Report DemonstrationI no written examination
I Week 04: Project introduction and forming of projectgroups
I Week 07: Submission of project plans by the projectgroups
I Week 08: Start of the projectI Week 13: Demonstration of the projects (each project 15
min)
![Page 30: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/30.jpg)
Course material
I Course Web page:http://www.imm.dtu.dk/courses/02161 contains
I practical information: (e.g. lecture plan)I Course material (e.g. slides, exercises, notes)I Check the course Web page regularly
I CampusNet: Is being used to send messages;I make sure that you receive all messages from CampusNet
I Books:I Textbook: Software Engineering 9 from Ian Sommerville
and UML Destilled by Martin FowlerI Suplementary literature on the course Web page
![Page 31: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/31.jpg)
Contents
Course Introduction
Introduction to Software Engineering
Practical Information
Eclipse, JUnit, and Exercises
User-defined Exceptions
![Page 32: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/32.jpg)
Programming Assignments
I Implementation of a library softwareI Guided development based on agile software development
principlesI User-story driven: The development is done based on user
stories that are implemented one by oneI Test-driven: Each user-story is implemented by first writing
the test for it and then writing the code
![Page 33: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/33.jpg)
Layered Architecture
Eric Evans, Domain Driven Design, Addison-Wesley,
2004
1. Development of the application +domain layer (assignments 1 – 4)
2. Presentation layer: Commandline GUI (assignment 5)
3. Simple persistency layer(assignment 6)
![Page 34: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/34.jpg)
First week’s exercise
I Using Test-Driven Development to develop the application+ domain layer
I Basic idea: First define the tests that the software has topass, then develop the software to pass the tests
I Writing tests before the code is a design activity, as itrequires to define the interface of the code and how to usethe code, before the code is written
I Test are automatic using the JUnit frameworkI First Week’s exercise: Tests are given, you implement just
enough code to make the tests pass→ Video on the home page of the courseI This is done by uncommenting each test one after the
otherI First implement the code to make one test run, only then
uncomment the next test and make that test run
![Page 35: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/35.jpg)
JUnit
I JUnit is designed by Kent Beck in Erich Gamma to allowone to write automated tests and execute themconveniently
I JUnit can be used standalone, but is usually integrated inthe IDE (in our case Eclipse)
I We are going to use JUnit version 4.x which indicates teststo be run automatically using the @org.junit.Testannotation (or just @Test if org.junit.Test is imported)
![Page 36: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/36.jpg)
Example of a JUnit Test
The following tests one scenario of the login functionality:1. First check that the adminstrator is not logged in2. login the adminstrator3. Check that the login operation returns the correct return
value (in this case true)4. Check with the system, that the user is logged in
@Testpublic void testLogin() {LibraryApp libApp = new LibraryApp();assertFalse(libApp.adminLoggedIn());boolean login = libApp.adminLogin("adminadmin");assertTrue(login);assertTrue(libApp.adminLoggedIn());
}
![Page 37: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/37.jpg)
JUnit: Creating new Eclipse projects I
I With JUnit 4.x every class can have tests by justannotating the method with @Test
I However, I suggest to separate tests from the source codeby putting them into their own source folder
I This can be done either on creation time or byI Using the properties dialog (selecting Java Build Path and
then Source)
![Page 38: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/38.jpg)
JUnit: Creating new Eclipse projects II
I In addition, the JUnit 4 libraries have to be available in theproject. This can be done again in the properties dialog(selecting Java Build Path and then Libraries)
![Page 39: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/39.jpg)
Eclipse code hintI Eclipse helps with Test-Driven Development by offering
help to fix the code, e.g. implementing missing classes andmethods
I In the first test case, Eclipse does not know the classLibraryApp and proposes to create it if one clicks on thelight bulb .
I Make sure that the source folder ends with src and not test
![Page 40: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/40.jpg)
Contents
Course Introduction
Introduction to Software Engineering
Practical Information
Eclipse, JUnit, and Exercises
User-defined Exceptions
![Page 41: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/41.jpg)
User-defined Exceptions
I Purpose: To notify the caller about some exceptional orerror state of the methodpublic void addBook(Book book)
throws OperationNotAllowedException {if (not(adminLoggedIn())throw new OperationNotAllowedException(...);
...}
I Creating a user defined exceptionpublic class OperationNotAllowedException extends Exception {public OperationNotAllowedException(String errorMsg) {
super(errorMsg);}
}
I Throwing a user-defined exceptionthrow new OperationNotAllowedException("some error message");
![Page 42: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/42.jpg)
Checked vs. unchecked Exceptions
I Checked Exceptionpublic class MyCheckedException extends Exception {...}
→ Methods which throw MyCheckedException must havethrows MyCheckedException in the signature, e.g.public void m() throws MyCheckedException {...}
I Unchecked Exceptionpublic class MyUncheckedException extends Error {...}
→ Methods don’t need the throw clause
![Page 43: Software Engineering I (02161) · 2013. 2. 4. · The course I 5 ECTS course 02161: Software Engineering 1 I Target group: Bachelor in Software Technology and IT and Communication](https://reader033.fdocuments.us/reader033/viewer/2022053022/604feab1347e5e662d112557/html5/thumbnails/43.jpg)
User-defined Exceptions: ExampleI Catching an user-defined exception
try {// Some block of code
} catch (OperationNotAllowedException e) {// Error handling code}
I Test for the presence of an exception@Testpublic void testSomething() {...try {// Some code that is expected to// throw OperationNotAllowedExceptionassertFalse(libApp.adminLoggedIn());libApp.addBook(b);fail("Expected OperationNotAllowedException to be thrown");
} catch (OperationNotAllowedException e) {// Check, e.g., that the error message is correctly setassertEquals(expected, e.getMessage());
}}
I Alternative test@Test(expected=OperationNotAllowedException.class)public void testSomething() {...}