BULME-6ABELI / Mbed 2 deprecated demirtas

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
kafka
Date:
Tue Apr 21 18:37:00 2020 +0000
Parent:
0:e0dea4ed78aa
Commit message:
aufgabe

Changed in this revision

beispiel_fur_exceptionklassen/exception.h Show annotated file Show diff for this revision Revisions of this file
beispiel_fur_exceptionklassen/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/beispiel_fur_exceptionklassen/exception.h	Tue Apr 21 18:37:00 2020 +0000
@@ -0,0 +1,40 @@
+#include <exception>
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+class myexceptions : public std::exception
+{
+  public:
+    enum class Error_Type {EVEN, TOO_SMALL};
+  private:
+    Error_Type error_type_;
+  public:
+    myexceptions(Error_Type error);
+    myexceptions(myexceptions const &var);
+    virtual const char *what() const noexcept;
+    ~myexceptions();
+};
+
+myexceptions::myexceptions(Error_Type error) : error_type_ (error)
+{
+}
+
+myexceptions::~myexceptions()
+{
+}
+
+const char* myexceptions::what() const noexcept
+{
+  switch (error_type_)
+  {
+    case Error_Type::EVEN:
+       return "error: number is even";
+    case Error_Type::TOO_SMALL:  
+      return  "error: number is too small";
+    default:
+      return NULL;
+  }
+  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/beispiel_fur_exceptionklassen/main.cpp	Tue Apr 21 18:37:00 2020 +0000
@@ -0,0 +1,30 @@
+#include "exception.h"
+#include <iostream>
+
+using std::cout;
+using std::endl;
+using std::cin;
+
+int main(int argc, char const *argv[])
+{
+  int number;
+  std::string input;
+  const std::string bb;
+  cout << "let's begin with exceptions" << endl;
+  cout << "write a number, which is not even and greater than 23: ";
+  try
+  {
+      cin >> number;
+      if (!(number % 2))
+      {
+        throw myexceptions(myexceptions::Error_Type::EVEN);
+      }
+      if(number < 23)
+        throw myexceptions(myexceptions::Error_Type::TOO_SMALL);
+  }
+  catch(const myexceptions& e)
+  {
+    std::cerr << e.what() << '\n';
+  }
+  return 0;
+}
\ No newline at end of file