LAB Practical V

a) Write a program to show the working mode of try, throw and catch blocks in C++

Comments

  1. AIM:-Write a program to show the working mode of try, throw and catch blocks in C++.
    Procedure:-
    Exception handling is used to find the errors which occurs during run time or execution time such as data overflow , arithematic exceptions.
    Exception handling is done by three major blocks:-
    Try:- is used to store error causing programs.
    Throw:-is used to throw values whenever an exception occurs.
    Catch:-is used to catch the thrown values and also contain the problem solving code.
    In the program the condition is x is not equal to 2,if the condition is true then it will throw x and catch code will be executed whereas if the condition is not satisfied it will not throw and further calculations will take place.
    Program:-
    #include
    using namespace std;
    int main()
    {
    int a,b;
    cout<<"enter values of a and b \n";
    cin>>a>>b;
    int x = a-b;
    try
    {
    if (x != 2)
    {
    cout <<" result (a/x)="<<a/x<<"\n";
    }
    else
    {
    throw(x);
    }
    }
    catch (int i)
    {
    cout<<"exception caught:x="<<x<<"\n";
    }
    cout <<"END";
    return 0;
    }
    Result:-
    Enter values of a and b:-
    4 and 2
    Exception caught:- x=2..

    https://drive.google.com/open?id=1gRh1MwZDrGMGpLB9rnMu34rQaohqPJKD

    ReplyDelete
  2. AIM: To show the working mode of try ,throw and catch block in C++.

    PROCEDURE: By using try, catch and throw i make this program. Try and catch block is referred as protected code . In this program i used two even number to check whether it is even or not .I have done this program inside the main function.

    PROGRAM:
    #include
    using namespace std;
    int main()
    {
    int a,e,o;
    cout <<"enter any value of a:";
    cin>>a;
    try
    {
    if(a%2==0)
    {
    cout<<"it is even:";
    throw(e);
    }
    else
    {
    cout<<"it is odd:";
    throw(o);
    }
    throw(a);
    }
    catch(int c)
    {
    cout<<"it is possible:";
    return 0;
    }
    }
    OUTPUT: enter any value of a 42
    it is even:
    enter any value of a 43
    it is odd:
    RESULT: I have done my program successfully.

    ReplyDelete
  3. AIM : TO create a program to show the working mode of try, throw and catch blocks in C++.
    PROCEDURE :
    step 1 : Start the program.
    step 2 : Try block is intended to throw exceptions, which is followed by catch blocks. Only one try block.
    Step 3 : It is used to throw exceptions to exception handler i.e. it is used to communicate information about error. A throw expression accepts one parameter and that parameter is passed to handler.
    Step 4 : Catch block is intended to catch the error and handle the exception condition. We can have multiple catch blocks.
    Step 5 : End the program.
    PROGRAM :
    #include
    using namespace std;
    int main()
    {
    int x[3]={-1,2,};
    for(int i=0;i<2;i++)
    {
    int ex=x[i];
    try
    {
    if (ex > 0)
    throw x;
    else
    throw 'ex';
    }
    catch (int ex)
    {
    cout << " Integer exception" ;
    }
    catch (char ex)
    {
    cout << " Character exception" ;
    }
    catch (...)
    {
    cout << "Special exception";
    }
    }
    return 0;
    }
    Error : 0
    Output :
    Integer exception Character exception.
    Result :
    I have successfully done my program.

    ReplyDelete
  4. AIM:write a program to show the working mode of try,catch and throw blocks in C++.
    PROCEDURE:
    EXCEPTION HANDLING is a mechanism that separates code that detects and handles exceptional circumstances from the rest of the program.
    It is built upon 3 keywords;
    1) throw: a program that throws an exception.
    2) catch: catches an exception with an exception handler
    3) try :identifies a block of code for which particular exceptions will be activated.
    In this program the condition m is equal to -1,if the condition is true then it will throw m and catch code will be executed whereas if the condition is not satisfied it will not throw and further calculations will take place.
    #include
    using namespace std;
    int main()
    {
    char l,m;
    try
    {
    cout<<"enter two characters";
    cin>>l>>m;
    if(m==-1)
    throw 1;
    cout<<"m=";
    }
    catch(char n)
    {
    cout<<"value is not possible";
    }
    }
    RESULT:
    Enter two characters
    P q
    M=
    Error=0

    ReplyDelete
  5. AIM:write a program to show the working mode of try,catch and throw block in c++.
    Exception handling is a mechanism that separates code that detects and handles exceptional circumstances from the rest of your program.It works with the help of three blocks;
    Try: identifies a block of code for which particular exception will be activated.
    Throw: it throws an exception .
    Catch: it catches an exception.
    PROCEDURE: in the program the condition y=3,if the condition is true then it will throw the value y and catch code will be executed.whereas if the condition is false then it will not throw the value and further calculations will take place.
    #include
    using namespace std;
    int main()
    {
    int x,y;
    try
    {
    cout<<"enter two numbers";
    cin>>x>>y;
    if(y==3)
    throw 3;
    cout<<"division"<<x/y;
    }
    catch(int a)
    {
    cout<<"division is not possible";
    }
    }
    Result ;
    Enter two numbers 6
    4
    Division1
    I have done my program successfully

    ReplyDelete
  6. AIM: To create a program working mode of try, throw and catch block in c++.
    PROCEDURE:
    • throw − A program throws an exception when a problem shows up. This is done using a throw keyword.
    • catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
    • try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
    PROGRAM:
    #include
    using namespace std;
    int main()
    {
    int n1,n2,result;
    cout<<"enter 1st number:";
    cin>>n1;
    cout<<"enter 2nd number:";
    cin>>n2;
    try
    {
    if(n2==0)
    throw n2;
    else
    {
    result=n1/n2;
    cout<<"the result is:";
    }
    }
    catch(int x)
    {
    cout<<"can't divide by:"<<x;
    }
    cout<<"end of program.";
    }
    ERROR: 0
    OUTPUT: Enter 1st no:45
    Enter 2nd no: 0
    Can’t divided by:0
    End of program.


    ReplyDelete
  7. AIM: Write a program to show the working mode of try, throw and catch blocks in C++.
    PROCEDURE: Exception handling is used to find the errors which doesnot appear in syntax error such as data overflow, arithematic exceptions.
    It is done by three blocks try, catch, throw.
    In this program I have created an exception using try and then calculated percentage and determined weather the student is fail or pass.
    PROGRAM:
    #include
    using namespace std;
    int main()
    {

    int percent;
    cout<<"Get the student percentage \n";
    cin>>percent;
    try
    {
    if(percent!=0&&percent<100)
    {

    cout<<"The detail is valid \n ";
    }
    else
    {
    throw(percent);
    }
    }
    catch(int m)
    {
    cout<<"exception caught "<<"\n";
    }
    cout<<"END";
    return 0;

    }
    OUTPUT:
    Enter student percentage
    34
    The detail is valid
    END
    OR
    Enter student percentage
    0
    Exception caught
    ERROR :0
    I have done my program correctly.

    ReplyDelete
  8. AIM : TO create a program to show the working mode of try, throw and catch blocks in C++.
    Procedure: Exception handling is used to find the errors which occurs during run time or execution time such as data overflow , arithematic exceptions.
    Exception handling is done by three major blocks:-
    Try:- is used to store error causing programs.
    Throw:-is used to throw values whenever an exception occurs.
    Catch:-is used to catch the thrown values and also contain the problem solving code.
    In the program the condition is x is not equal to 2,if the condition is true then it will throw x and catch code will be executed whereas if the condition is not satisfied it will not throw and further calculations will take place.
    Program:
    #include
    using namespace std;
    int main()
    {
    float pi=3,14;
    float r,area;
    cout<<"Enter radius to calculate area of the circle"<<endl;
    cin<<r;
    try
    {
    if (r<0)
    throw"Negative radius not possible";
    area= pi*r*r;
    }
    catch(char* ex)
    {
    cout<<"area is: "<<area;
    return 0;
    }
    Error: No error found.
    Output: Enter radius to calculate area of the circle
    3
    area is: 28.26
    Result: This program execute successufully.

    ReplyDelete

Post a Comment

Popular posts from this blog

INTRODUCTION OF HADOOP & SPARK

B.TECH DIP- System Programming