0% found this document useful (0 votes)
7 views2 pages

Krishiv OOPS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Krishiv OOPS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

JAGRAN LAKECITY UNIVERSITY

Name: KRISHIV SHARMA


Roll no.: 2023BTCSE007
JLUid : JLU08441

Write a program containing a Possible Exception. Use a Try Block to throw it and a Catch
Block to Handle it Properly.

#include<iostream>

#include<stdexcept>

double divide(int numerator,int denominator){

if(denominator==0){

throw std::invalid_argument("error :division by zero is not allowed");

return static_cast<double>(numerator)/denominator;

int main(){

int num,denom;

std::cout<<"enter numerator";

std::cin>>num;

std::cout<<"enter denominator";

std::cin>>denom;

try{
// Trying to divide

double result = divide(num, denom);

std::cout << "Result: " << result << std::endl;

catch (const std::invalid_argument& e) {

// Handling the exception

std::cout << "Caught exception: " << e.what() << std::endl;

return 0;

//OUTPUT

You might also like