Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throwing Exceptions

Status
Not open for further replies.

tommyboyau

Programmer
Feb 7, 2005
47
0
0
AU
Hi everyone,
I need some clarification with throwing exceptions. I'm interested in two things:
x Throwing exceptions which completly stop the program running
x Throwing exceptions which but the program keeps running.

eg: In the first instance the program might not recieve the correct amount of command line arguments and have to stop all together.

In the second instance the program might check that an integer is a certain value. If its not that value then it sets it to say 1 and continues operation.

If someone could provide some examples or some help that would be great.

Thanks,

Tom
 
let me try, dunno if it is what you are asking.

for the second instance
Code:
public int function(String variable)
{
   int x;
   try
   {
      x=Integer.parseInt(variable);
   }
   catch(Exception e)
   {
    //supposing variable is invalid number
    x=0;
   }

   //continue rest of coding, eg apply formula on x
   return x;
}
 
@lowbk: not best style, to catch an anonymous Exception.

catch (NumberFormatException e)

should be used.

@tommyboyau
Whether the program will stop working or not doesn't depend on the thrown Exception, but on the question whether you catch it or not.

If you expect an exception with reasonable probability, you should catch it, because then you may display a message, telling the user how to fix the misbehaviour, and exit gracefully.

Instead of using a default-value silently, it would be better to ask the user for corrected input.

seeking a job as java-programmer in Berlin:
 
This is what i'm after.
My program reads a file. Line one will be a plain number.
If the number on line one is less then 2 or greater then 20 for example, the program should throw an exception BUT continue operation.

There are other examples where I want the program to stop operating as it cannot continue and I can do that, I just dont know how to raise an exception but continue operation.

Does that make sense?
 
Not for me.

At the moment you throw an exception, you lose the control of the code. The operation will continue, of course, but at the point where the exception is caught.

I use to throw exceptions when the error is unrecoverable for a normal functioning. Then I catch the exception at an external point, free all resources and send a message to the user

Cheers,
Dian
 
@tommyboyau
Why do you want to throw an exception, if you want to preceed too?
Are you looking for
Code:
while (...)
{
    if (n < 2 || n > 20)
        System.err.println ("invalid input: " + value);
    else
    {
         // real work
    }
}

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top