Skip to content

Commit

Permalink
Add:Exception Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
muhibarshad committed Dec 1, 2022
1 parent 0037119 commit 1c33324
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*There are many ways to achieve the exception handling.
By simple try, throw any dataType , catch it
By using multiple catches the throwed dataType matched catch block will exe.
By using catch(...){} it can exe. default it can exe any throw which is firstly throwed
BY throwing object use refernce of object in catch block then call the static attached error function
By Inherited the Exception class to your error class and and pass the constructor msg to :exceptio(msg){} then override the virtual const char*what() const {} func by adding your won comments and then return the exception::what(); of the base class.
By using system ddefaut inherited classes like bad_alloc , out_ofRange like
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>

using namespace std;

int main()
{
int x, y;
cout << "Enter the two numbers :\n";
cin >> x >> y;
try
{
if (y != 0)
cout << "Divsion = " << x / y << "\n";
else
throw y;
}
catch (int i)
{
cout << "Caught y = " << i << " could not be be divisible\n";
}
return 0;
}
20 changes: 20 additions & 0 deletions Object Oriented Programming/Step8_ExceptionHandling/tryCatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

using namespace std;

int main()
{
cout << "Code start here\n";
try
{
cout << "First line executed:\n";
throw 90;
cout << "This line cannot be executed due to incedently throwing an error\n";
}
catch (double i)
{
cout << "The execption of line " << i << " can be executed\n";
}
cout << "Code ends here\n";
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>

using namespace std;

float divide(float x, float y)
{
if (y != 0)
cout << "Divsion = " << x / y << "\n";
else
throw y;
}

int main()
{
int x, y;
cout << "Enter the two numbers :\n";
cin >> x >> y;
try
{
divide(x, y);
}
catch (float i)
{
cout << "Caught y = " << i << " could not be be divisible\n";
}

return 0;
}

0 comments on commit 1c33324

Please sign in to comment.