[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

return value or exception

Posted on 2003-03-09
7
Medium Priority
?
145 Views
Last Modified: 2010-03-31
Hi all
I write a method which return an Object value:
 
  public Image convertImage(Image image,String terminal)
  {
      .........
      if (terminal is not supported) {
         ... what should I do here?
      }
  }

The method receive as input Image Object and target terminal indicate the terminal this image should be
displayed on. what should I do in the if statement if the required terminal is not supported by me?
should I return null and write a message to log file explain the terminal is not supported or should I throw an exception such UnSupportedTerminalException to indicate the exact error so the caller will know the problem?

What and how should I treat other errors:
1. suppose my method support JPEG and BMP only what should I do if I get other format?
2. If analyze the media and terminal I realize the terminal does not support the current image format?

   Thanks Danny
0
Comment
Question by:dannysh
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8101099
public Image convertImage(Image image,String terminal)
   throws UnsupportedOperationException
 {
     .........
     if (terminal is not supported) {
        throw new UnsupportedOperationException("reason");
     }
 }

0
 

Author Comment

by:dannysh
ID: 8101131
Hi objects
Can you explain me please why throwing an exception is the prefered way?

And what should I do with the other errors (in the bottom of the original question)
  Danny
0
 
LVL 92

Accepted Solution

by:
objects earned 160 total points
ID: 8101199
Thowing an exception in your case is better because it gives more details of why it failed to the caller.
If you just returned null, the caller would have no idea why it failed.

If you have different types of errors then you could throw different exceptions, containing details for the failure.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:dannysh
ID: 8101215
Hi Objects
First I want to thank you for your help.
Is there any alternatives to handle the situation or it's the best? (The class contains this method does not contains state (any member variables))
 
   Danny
0
 
LVL 92

Expert Comment

by:objects
ID: 8101226
Really the only alternative in your case is to return null, but this has the limitations that the caller has no idea why it failed.
0
 

Author Comment

by:dannysh
ID: 8101234
Thank you very much objects.
0
 
LVL 92

Expert Comment

by:objects
ID: 8101268
No worries :)

Happy coding.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question