< Prev
Next >



Exception Handling using try-catch block





In order to handle an exception thrown in a program, we can use the try-catch block. A try-catch block is made up of two individual blocks - a try block and a catch block.





The catch block declares a type of exception that it is going to catch and handle. If the declared exception-type in catch block is similar to the type of exception thrown by try block, the exception is caught within this catch block and the program continues its normal execution without ending abruptly.









How to catch an exception using try-catch block?


Exception thrown has a certain data-type. In order to catch an exception of a type, we have to make sure that our catch block has declared an exception-type similar to the data-type of exception that is thrown by the try block.


Let's understand this by an example -:
#include<iostream>

using namespace std;

int main()
{
int a=10, b=0, c;
try
{
	//if a is divided by b(which has a value 0);
	if(b==0)
		throw(c); 
	else
	c=a/b; 		
		
}
catch(int a) //matching int type of exception is declared in "catch block"
{
	cout<<"An exception caught : division by zero \n"; //message related to caught exception is printed
}		

cout<<"Outside try-catch block";
return 0;
} 		


Output :


An exception caught : division by zero
Outside try-catch block


As you can see in the program, it finds a division-by-zero problem and an exception of type int is thrown when a statement enclosed within the try block is executed. This exception is caught by the catch block because it is declared with a matching exception of type int.

As the thrown exception of type int is caught by the catch block, hence, the program does not end abruptly and it continues to successfully execute the statement next to the try-catch block, which prints a message - "Outside try-catch block".





Another example of using try-catch block


Let us see another example in which we are going to catch an out-of-range index exception, which gets thrown when a statement is trying to access an index of array which is out of its range. Let us see how to deal with such exception using try-catch block.

Let's understand this by an example -:
#include<iostream>
#include<string>

using namespace std;

int main() 
{
char arr[5];
try
{
	for (int n=0; n<=6; n++)
    	{
     		 if (n>4) 
			throw std::string("out-of-range");
     		 else 
			arr[n]='a';
    	}
}
catch (std::string st)
{
	std::cout << "Exception caught : " << st;
}
return 0;
}


Output :


Exception caught : out-of-range


As you can see in the program, the compiler finds one of the statements enclosed within the try block is trying to access an invalid-index or out-of-range index hence, an exception of type std:string is thrown This exception is caught by the catch block because it is declared with a matching exception of type std:string.





Mismatch in exception declared and exception thrown





Let us understand this by an example here.
#include&klt;iostream>

using namespace std;

class TryCatch2
{
public: 
void meth();
};



void TryCatch2 :: meth()
{
try
{
	int a = 10;
	int b = 10;

	int result = a-b;
	if(result==0)
	 	throw(b);
	else
	cout<<(100/result);
}
catch(double c) //Non-matching exception-type is declared in this catch block
{
	cout<<"Caught exception type : double type \n";
}
cout<<"Outside try-catch block";
} //meth1() function ends




int main()
{
TryCatch2 ob;
try
{
	ob.meth(); 	//enclosing the call to meth1() function in try block
}
catch(int c)
{
	cout<<"Caught exception : int type \n"	;
}

} //main function ends


Output:


Caught exception : int type
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2