Question

.)   Write a program to implement the exception handling with multiple (at least 3) catch statements....

.)   Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown.

.)   Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()".

(This is pretty open ended, so anything from a calculator or "what have you" will do. I do appreciate the help fully.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program 1:

#include<iostream>
using namespace std;

int main()
{
int a;
cout<<"Enter value of a : ";
cin>>a;

try
{
if(a==1)
{
throw a;
}
else if(a==2)
{
throw 'a';
}
else if(a==3)
{
throw a+1.5;
}
else
{
throw "";
}
}
catch(int x)
{
cout<<"Exception related to integer : "<<x;
}
catch(char x)
{
cout<<"Exception related character : "<<x;
}
catch(double x)
{
cout<<"Exception related to double : "<<x;
}
catch(char* x )
{
cout<<"Nothing is there : "<<x;
}
cout<<endl;
system("pause");
return 0;

}

usenDeskt oplDeb Enter value of a: 1 Exception related to integer1 Press any key to continue .. . Activate Windows Go to Sett

Program 2:

#include<iostream>
using namespace std;

int showElement(int arr[5],int index)
{
return arr[index];
}

int main()
{
int arr[5],i;
int index;

for(i=0;i<=4;i++)
{
cout<<"Enter element at "<<i<<" : ";
cin>>arr[i];
}

cout<<"\n\nEnter Index : ";
cin>>index;

try
{
if(index>sizeof(arr))
{
throw index;
}
else
{
cout<<"\n\nElement at index "<<index<<" is "<<showElement(arr,index)<<endl;
}
}
catch(int x)
{
cout<<"Index out of bound exception !!"<<x;
}
cout<<endl;
system("pause");
return 0;

}

Enter element at 0 23 Enter element at 134 Enter element at 2 45 Enter element at 3 56 Enter element at 4 23 Enter Index2 Ele

Add a comment
Know the answer?
Add Answer to:
.)   Write a program to implement the exception handling with multiple (at least 3) catch statements....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is...

    QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is no catch block handling the same data type as the exception thrown execution continues if the catch block handles the same data type as the exception thrown 10 points    QUESTION 2 An exception is handled using an if-else statement an assignment statement a loop statement a try-catch block 10 points    QUESTION 3 In program 9.4, what is the data type being thrown?...

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from...

    CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from Chapter 12. There is no need for any UML for this lab. Provide your implementation (java source code) and the output for each of the tests. There should be one class for exercises 12.2 and 12.3 and two classes for 12.4. I prefer one output file per programming exercise. For each problem you must handle the exception. 12.2 (InputMismatchException) Write a program that prompts...

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal...

    Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. -- Not needed Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. --needed ZeroException Task: -- Not needed Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise....

  • absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed...

    absolute C++ QUESTION 1 Which statement is incorrect? When an event occurs that cannot be managed locally, an exception may be thrown. Throwing the exception object transfers control and information gleaned locally to some calling program unit that manages the event, or the program terminates If an exception is thrown in a function, sayf, but not handled there, the exception is propagated to the function that called fo In C++, an exception object can be a user-defined type or any...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT