?
Solved

return value or exception

Posted on 2003-03-09
7
Medium Priority
?
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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