Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. class A {
  2. private:
  3. QFile file;
  4.  
  5. public:
  6. A::A(QFile file): file(file) {}
  7.  
  8. void doSomething() {
  9. file.open(QIODevice::WriteOnly);
  10. // ... do operations that can throw an exception
  11. file.close();
  12. }
  13. }
  14.  
  15. class A {
  16. private:
  17. QFile file;
  18.  
  19. public:
  20. A::A(QFile file): file(file) {}
  21.  
  22. void doSomething() {
  23. file.open(QIODevice::WriteOnly);
  24. try {
  25. // ... do operations that can throw an exception
  26. }
  27. finally {
  28. file.close();
  29. }
  30. }
  31. }
  32.  
  33. void A::doSomething()
  34. {
  35. QFile file;
  36. file.open(...);
  37. // ...
  38. file.close(); // So you can check that everything when right.
  39. }
  40.  
  41. class QFileWrapper
  42. {
  43. QFile* myFile;
  44. public:
  45. QFileWrapper( QFile* file ) : myFile( file ) {}
  46. ~QFileWrapper() { myFile->close(); }
  47. };
  48.  
  49. try {
  50. /*do something that may throw an exception*/
  51. } catch (.../*the ellipsis means catch anything*/){
  52. /* any cleanup */
  53. throw; /*this rethrows the exception, not compulsory to include*/
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement