×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Catching an exception

    Hello,

    In the midlet I've done, when it sends a SMS to a invalid phonenumber (i.e, 111222), there is a system error alert that says "Message sending failed".

    I am wondering if I can monitor this alert, because when it appears, I can't control the display that is visible, so the user can navigate correctly through the application.

    I am controlling this exceptions

    try
    {
    smsconn.send(sms);
    }
    atch (Exception exception)
    {
    if (exception instanceof SecurityException)
    { /* do something */ }
    if (exception instanceof IOException)
    { /* do something */ }
    if (exception instanceof IllegalArgumentException)
    { /* do something */ }
    if (exception instanceof InterruptedIOException)
    { /* do something */ }
    if (exception instanceof NullPointerException)
    { /* do something */ }
    }

    but the midlet does another thing.

    Any guess about what I am missing?

    Regards

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    112
    its the exception syntax that is a bit weird. try it the standard way:

    try
    {
    smsconn.send(sms);
    }
    catch(SeurityException secex)
    { /* do something */ }
    catch(IOException ioex)
    { /* do something */ }
    catch(IllegalArgumentException iargex)
    { /* do something */ }
    catch(InterruptedIOException iioex)
    { /* do something */ }
    catch(NullPointerException nex)
    { /* do something */ }
    catch (Exception ex)
    {
    // do something here
    }
    finally{
    // ne exceptions until here
    // do something anyway
    }

    note: the order in which you catch the excteption is important. special exceptions first, generic exceptions last.

    errors are not catchable (memory error). but such sms-sending failed system errors should cause exceptions.
    thing is what exception is thrown.

    in the catch(Exception ex)
    you may print the exception to screen and see which one you got.

    good luck

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    According to the specification for send(), you are catching everything that it can throw.

    Remember that a catch() clause will match an exception of the specified type, or an exception of any subclass of the specified type. So there is no point catching IOException, then later trying to catch InterruptedIOException, as InterruptedIOException is a subclass of IOException. For example:
    Code:
    try {
        throw new InterruptedIOException ();
    }
    catch (IOException e) {
        // the exception will get caught here, because
        // InterruptedIOException is a subclass of IOException
    }
    catch (InterruptedIOException e) {
        // this is unreachable
    }
    Because the code in the second catch() clause is unreachable, I doubt that this would even compile... you've got around that by sorting exceptions out in a different way. But the result is similar.
    Code:
    try {
        throw new InterruptedIOException ();
    }
    catch (Exception e) {
        // any subclass of Exception will get caught here
        if (e instanceof {
            // an InterruptedIOException is an instance of
            // IOException, so this code gets executed
        }
        if (e instanceof InterruptedIOException) {
            // because there is no "else", this gets executed too!
            // if it was written "else if" then this would be
            // unreachable
        }
    }
    You can make sure that you catch everything that can possibly be caught, by catching everything that can be thrown:
    Code:
    try {
        // do stuff here
    }
    catch (Throwable t) {
        // if anything is thrown, you will catch it here
    }
    Of course, if a failed send() doesn't throw anything, there'll be nothing to catch. Message objects have a timestamp property, indicating when they were sent... what value does this have if the Message isn't sent? (The specification doesn't say, so it may not be reliable).

    Graham.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •