Question: When we call a method that might throw an exception, sometimes the right thing to do is to catch it , and other times we

When we call a method that might throw an exception, sometimes the right thing to do is to catch it, and other times we should let it be thrown out of our method.
But sometimes we want to catch that exception and save it for later, to be dealt with by another part of our program. This is particularly useful when the code that is using the result of the method call is not the caller of the method, a situation that we'll encounter once we begin Android development.
To help handle this situation, create a public class called ResultMightThrow. It stores the result of calling a method that might throw: either an Object result if the method succeeded, or an Exception exception if it threw. ResultMightThrow provides two constructors: one accepting a single Object, the other a single Exception. The Object may be null, but you should reject null exceptions by throwing an IllegalArgumentException.
You should provide a getter for the result using our usual naming convention. However, if the ResultMightThrow actually stores an exception, you should throw that exception instead when getResult is called. getResult must be declared to throw an Exception.
You should also provide a getter for the exception, again following our usual naming conventions. When complete, here is how your class should work:
1// When callThatMightThrow doesn't throw
When we call a method that might throw an

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!