exceptions handling.

Hi,
I need to write implement exceptions, that prints error message to the screen, that appropriate to the LibraryItem kind, Book or ReservedBook.
what changes should i do to the borrowTitle function ?
my function didn`t refer to differences between Book and ReservedBook and Jorunal.
note: couts with ******** are the expected outputs.

main():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	library.borrowTitle(s2.getId(), b2.getCatalogId());
	library.borrowTitle(s2.getId(), r2.getCatalogId());
	library.borrowTitle(s2.getId(), j2.getCatalogId());
	try
	{
		library.borrowTitle(s2.getId(), b1.getCatalogId());
	}catch(exception& e){
		cout<<e.what()<<endl;
	}
	cout<<"book with catalogid 1 is already borrowed! **********"<<endl;
	try
	{
		library.borrowTitle(s2.getId(), r1.getCatalogId());
	}catch(exception& e){
		cout<<e.what()<<endl;
	}
	cout<<"reserved book with catalogid 2 is already borrowed! **********"<<endl;
	try
	{
		library.borrowTitle(s2.getId(), j1.getCatalogId());
	}catch(exception& e){
		cout<<e.what()<<endl;
	}
	cout<<"journal with catalogid 3 is already borrowed! **********"<<endl;


birriwTitle():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Library::borrowTitle(const long id, const long catNum){
	try{
		list<Student>::iterator stud;
		stud = find_if(students.begin(),students.end(),searchStudent(id));
		if (stud == students.end())
			throw StudentNotFound();
		list<LibraryItem*>::iterator itm;
		itm = find_if(items.begin(),items.end(),searchItem(catNum));
		if (itm == items.end())
			throw ItemNotFound();
		if (stud->findInLoanList(catNum))
			throw AlreadyBorrowedByStudent();
		stud->addToLoanList(*itm);
		(*itm)->setLoaningDate(today);
		(*itm)->setLoanStatus(true);
	}
	catch(ItemNotFound& e){
		cout << e.what() << " (id=" << catNum << ")" << endl;
	}
	catch(StudentNotFound& e){
		cout << e.what() << " (id=" << id << ")" << endl;
	}
	catch(AlreadyBorrowedByStudent& e){
		cout << "student " << id << " " << e.what() << "catalogid " << catNum << endl;
	}
}


LibraryItem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef _LIBRARYITEM_H_
#define _LIBRARYITEM_H_

#include <iostream>
#include <string>
#include <time.h>

using namespace std;
class LibraryItem {
public:
	//Consturcor
	LibraryItem(long cNum = 0, string _bookName = "", int days = 0): catalogNumber(cNum), bookName(_bookName), maxLoaningDays(days), isLoaned(false){};
	virtual long getCatalogId() const { 
		return catalogNumber;
	}
	virtual void setLoanStatus(bool st){
		isLoaned = st;
	}
	virtual bool getLoanStatus(){
		return isLoaned;
	}
	virtual void setLoaningDate(time_t today){
		loaningDate = today;
	}
	virtual time_t getLoaningDate() const{
		return loaningDate;
	}
	virtual int getMaxDays() const{
		return maxLoaningDays;
	}

	//Distructor
	virtual ~LibraryItem(){}
private:
	long catalogNumber; 
	string bookName;
	bool isLoaned;
	const int maxLoaningDays;
	time_t loaningDate;
};	
#endif 


ReservedBook;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _RESERVEDBOOK_H_
#define _RESERVEDBOOK_H_

#include <iostream>
#include <string>
#include <time.h>
#include "LibraryItem.h"

using namespace std;
class ReservedBook: public LibraryItem {
public:
	//Consturcor
	ReservedBook(long cNum = 0, string _bookName = "", string _authorName = ""): LibraryItem(cNum,_bookName,3), authorName(_authorName) {}
	//Distructor
	virtual ~ReservedBook(){}
private:

	string authorName;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef _BOOK_H_
#define _BOOK_H_

#include <iostream>
#include <string>
#include <time.h>
#include "LibraryItem.h"

using namespace std;
class Book: public LibraryItem {
public:
	//Consturcor
	Book(long cNum = 0, string _bookName = "", string _authorName = ""): LibraryItem(cNum,_bookName,14), authorName(_authorName) {}
	//Distructor
	virtual ~Book(){}
private:

	string authorName;

};
#endif


Journal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _JOURNAL_H_
#define _JOURNAL_H_

#include <iostream>
#include <string>
#include <time.h>
#include "LibraryItem.h"

using namespace std;
class Journal: public LibraryItem {
public:
	//Consturcor
	Journal(long cNum = 0, string _bookName = "", int _copyNumber = 0): LibraryItem(cNum,_bookName,7), copyNumber(_copyNumber) {}
	//Distructor
	virtual ~Journal(){}
private:

	int copyNumber;
};	
#endif 


exception.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class StudentNotFound: public exception {
public:
	StudentNotFound(){}
	virtual const char* what() const {
		return "Student NOT exists";
	}
};

class ItemNotFound: public exception {
public:
	ItemNotFound(){}
	virtual const char* what() const {
		return "Item NOT exists";
	}
};

class ItemAlreadyLoaned: public exception {
public:
	ItemAlreadyLoaned(){}
	virtual const char* what() const {
		return "Item Already Borrowed";
	}
};

class AlreadyBorrowedByStudent: public exception {
public:
	AlreadyBorrowedByStudent(){}
	virtual const char* what() const {
		return "already borrowed ";
	}
};

class NotBorrowedByStudent: public exception {
public:
	NotBorrowedByStudent(){}
	virtual const char* what() const {
		return "Item is not borrowed by student";
	}
};
Try to phrase your question more clearly.
"Exceptions" shouldn't print anything on the screen. Exceptions should break your program, go to the closest catch() and carry some information about why the exception occurred. That might be an error message that you may decide to print on your screen, but the exception itself doesn't print anything.
if i want to borrowTitle that already borrowed, the function have to throw a message says that the title is already borrowed, bout it need to tell if it a book, reservedbook or journal. (like what in the couts with *******).
You can do that by passing the relevant parameters (the catalogid or whatever) to the exception's constructor, which can then construct a string out of it (which you need to store in the class). Use c_str() to return the string in what().
Note: you might want to inherit from std::runtime_error:
http://www.cplusplus.com/reference/std/stdexcept/runtime_error/
Last edited on
hanst99: i know that about exception, and my ques what should i do in order to make the function throw an exception for every kind of item.
1
2
3
4
5
6
7
try{
throw runtime_error("your message");
}
catch(runtime_error e&)
{
cout<<e.what();
}
thnx, i added throw and exception with string kind, and the problem resolved.
thank you so much guys.
Note that you don't need to throw and catch in the same block:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void throwError()
{
throw runtime_error("the error");
}

int main()
{
try
{
  throwError();
} catch(runtime_error e&)
{
   cout<<e.what();
}
}
Topic archived. No new replies allowed.