-1

I want to understand how come the below program is running without throw keyword ,when should we use throw with try-catch block.

#include <iostream>
#include <exception>
using namespace std;

int main () {
try{
long double * arr= new long double[1500000000000000000000000000000000000];
cout<<"hello,This is an exception handling program"<<endl;
//throw exception();
}
catch (exception& e){
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}

Output
Standard exception: std::bad_array_new_length
Vibhu
  • 1
  • 3

1 Answers1

1

You don't need to throw anything for try catch to work; take a look at these links. Also this question has been asked and answered here many times.

https://msdn.microsoft.com/en-us/library/hh279678.aspx

https://msdn.microsoft.com/en-us/library/6dekhbbc.aspx

The following link will give you a fairly decent explanation.

How to throw a C++ exception

If you actually took the time to go through those you'd have seen that some of the examples show use of a throw while others do not. So back to basics.

try
{
    //throw something;
}
catch(...)
{
    //do something
}

The above snipet of code represents the following ideas.

If your code can potentially cause an exception, your code should be placed inside of a try block.

The catch portion catches the exception if one occurs.

You will either create your own exception handling class or use someone else's. It's relative to what you're working with.

The idea is quite simple; Try to do this and if it causes an exception catch it and do this so my program doesn't crash.

So, what's a throw and why do I not need one?

A throw statement is you directly throwing an exception. Period end of story. Think about like this. You have a function that divides two numbers, the numbers are based off user input. if the user enters two numbers and one of them is 0 and the other is say 25.

It would cause an exception because of math.... You could then anticipate that and throw that division by zero exception to prevent a fatal crash.

An exception must occur for it to be caught. An exception can be anything you want it to be, and or something you don't want.

What I mean to say is you can throw something that you created that works only with your program.

You asked, why will this code run with out a throw?

A try catch block does not need to have an exception occur. If an exception happens it happens wether you throw it or not. Thus a try catch block does NOT require a throw.

For instance.

try
{

}
catch(...) // This is a real thing. It's meant to catch everything but don't use it becasue it's bad.
{

}


try 
{
  throw("An exception happened")
}
catch(...)
{
  cout << "Nothing actually happened" <<endl; // This block is valid..
}

IMPORTANT: I'm not telling you to not use a throw. I'm simply answering your question and explaining why you don't need one for a try catch to run.

Community
  • 1
  • 1
suroh
  • 917
  • 1
  • 10
  • 26
  • :i went through your links but in those links clearly we are using throw keyword and then we are handling the exception but my question is how come in my code (above lines of code) catch block is getting executed even if i am not using throw keyword explicitly. thanks – Vibhu May 13 '17 at 16:29
  • Because throw is you taking the exception handling int your own hands and throwing an exception that you believe will potentially happen. Leaving out a throw will not prevent a catch from happening but the catch process is automatic if setup right. – suroh May 13 '17 at 20:31