I have a School
class which includes avl trees: one for students
, another for emptyClasses
, and another for nonEmptyclasses
.
The function I’m struggling with looks like this: (status
is an enum such that 0=succes,1=allocation_error,3=failure,4=invalid=input
):
Status leave_class(int studentId){
//check validity of input
try{
if(numStudents==1){
#remove class from nonEmptyClasses
#add class to emptyClasses
}
Class->removeStudent(studentId)
}
catch(...){
Return status::allocation_error
}
Students->leave() //this line changes student fields to ints of 0 and nullptr for pointers
Return status::successs
}
The 3 actions in the try
block could throw exceptions/errors, but then in the catch
block if I try to reverse the error that happened then the reversing process could also be prone to the same errors.
For example, if I succeeded in removing a class from nonEmptyClasses
and failed to add it in the emptyClasses
, I have to reverse the removal which could also end up with an error.
I thought of separating the actions into multiple functions, or using mltiple try
blocks, but I keep going back to square 1.
Any recommendations?
Iii is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.