Home > Back-end >  Are C exceptions expected to be handled in main()?
Are C exceptions expected to be handled in main()?

Time:05-31

I am doing a project in C , and I am new to OOP.

I have some doubts regarding C exceptions and where they have to be handled. I read that it is a good practice to insert the try/catch block into the main() function, letting the exceptions thrown into "deep placed" methods to climb up to main() and be handled there, programming the user feedback in such cases.

The problem is that not all the errors of the project are treated like exceptions, and for that reason high level functions called in main() return a bool value that is a first indicator of an error state.

So, I am wondering whether it is a good thing not to handle exceptions in main() in that case, placing the try/catch block into low-level methods and returning a false value in the high-level one when an exception has been caught in the former, allowing me to treat all the errors the same way.

CodePudding user response:

There is no general rule where these handlers should go.

But you describe your program as "high level functions called in main() return a bool value". That is a reasonable choice: any specific exception handling is taken care of by those high-level functions. From the perspective of main, these high-level functions either succeed or they fail. Hence, main does not need to bother with exceptions in your specific design.

  • Related