You Are Here:

Community: Developer Discussion Boards

#1 Old Catching an exception - 2003-10-10, 12:20

Join Date: Mar 2003
Posts: 25
btnokdev
Offline
Registered User
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
Reply With Quote

#2 Old 2003-10-11, 11:48

Join Date: Mar 2003
Posts: 112
ossipetz
Offline
me-2-j :)
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 :)
Reply With Quote

#3 Old 2003-10-11, 15:45

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
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.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia