Exception Handling in C++

  • Exception is an abnormal behavior of the program during runtime.
  • It will occur when the user gives input to the program something that is out of the scope.
  • To resolve this problem we can use exception handling, C++ Provide an inbuilt error handling mechanism. We also handle this without an inbuilt mechanism but that is not much efficient to handle the exception.
  • Keyword used in the exception handling:-
  •  try: Try is Block of code where the chance of exception occurs.
try {
 }
  • catch: Try is Block of code that can handle the exception when occurring in the try block.
catch(type argument)
{  }
  • throw: It is used in try block. It will throw the exception to the catch block.
try{
throw execption;
}

Untitled Diagram (1)

Syntax

try
{
  throw arg;
}
catch(type1 arg)
{
}
catch(type2 arg)
{
}

Leave a Reply

%d bloggers like this: