Exception Handling In C++

Card image cap

Exception Handling ?

In C++ Programming, Exception Handling is the best method for handles run-time errors.An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.


Errors can be broadly categorized into two types

1) Compile Time Errors
This error is occurred due to use of wrong idiom, function or structure. This error is shown at compilation time of program.
2) Run Time Errors
This error occurred at the run time. This error occurs when program crashes during run time.

Syntax

Card image cap

Example 1:



Output :

   Scenario 1:
   Enter the Number :10
   10

   Scenario 2:
   Enter the Number :0
   Exception Occurred : Exception Value: 0

   Scenario 3:
   Enter the Number :-1
   Exception Occurred : Exception Value: Negative Input
Card image cap

Example 2:



Output:

  
Enter sudentname :Abhigyan
Enter physics marks : 95
Enter chemistry marks : 98
Enter mathsmarks : 102
Invalid Marks

Enter sudentname :Abhigyan
Enter physics marks : 95
Enter chemistry marks : 198
Invalid Marks

Enter sudentname :Abhigyan
Enter physics marks : 125
Invalid Marks

Enter sudentname :Abhigyan
Enter physics marks : 95
Enter chemistry marks : 98
Enter mathsmarks : 99
Card image cap