Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Exception handling

Thread view: 
Sharp Tool - 30 Oct 2005 11:31 GMT
Hi,

I want to check if a statement throws an exception.
If it does, I want to deal with it then and there, and not at the catch
claus.
See code below:

...
String[] str = {"dinner"};
if (str[1].charAt(0) throws ArrayIndexOutOfBoundsException) // compile error
{
   //do something 1
}
else //do something 2

Is this possible?

Sharp Tool
Andrew McDonagh - 30 Oct 2005 12:02 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> Sharp Tool

only by catching or by using a querying method of some kind to determine
if an exception is likely to be thrown.

e.g

instead of ...

if (str[1].charAt(0) throws ArrayIndexOutOfBoundsException) // compile error

do...

if (str.length > 0)
  str[1].charAt(0)

This way - no ArrayIndexOutOfBoundsException will be thrown because of
the length  is too small we don't try and get the first element.

Google 'writing Exception Safe code'
Sharp Tool - 30 Oct 2005 12:21 GMT
> > Hi,
> >
[quoted text clipped - 31 lines]
> This way - no ArrayIndexOutOfBoundsException will be thrown because of
> the length  is too small we don't try and get the first element.

Actually thats whats I am doing, but i was wondering if you could actually
check if an exception would be thrown directly and explicity.

Sharp Tool
Malte - 30 Oct 2005 12:12 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> Sharp Tool

Why not:

try {
  // whatever
  // do something 2
} catch (ArrayIndexOutOfBoundsException e) {
  // deal with it, ie do something 1
}
Sharp Tool - 30 Oct 2005 12:30 GMT
> > Hi,
> >
[quoted text clipped - 23 lines]
>    // deal with it, ie do something 1
> }

Like i said I don't want to deal with the exception in the catch claus.
Doing so, will not return me to where I left off.

Sharp Tool
Thomas Hawtin - 30 Oct 2005 12:53 GMT
>[Malte wrote:]
>>
[quoted text clipped - 7 lines]
> Like i said I don't want to deal with the exception in the catch claus.
> Doing so, will not return me to where I left off.

You want the exception to still be thrown?

try {
    // whatever
    // do something 2
} catch (ArrayIndexOutOfBoundsException exc) {
    // do something 1
    throw exc;
}

However, you should rarely need to catch runtime exceptions. Much better
to ensure what you are doing is valid before.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 30 Oct 2005 19:33 GMT
On Sun, 30 Oct 2005 11:30:09 GMT, "Sharp Tool"
<sharp.tool@bigpond.net.au> wrote, quoted or indirectly quoted someone
who said :

>Like i said I don't want to deal with the exception in the catch claus.
>Doing so, will not return me to where I left off.

Exceptions are not like PL/I ON units.  You either have to write your
own code to detect the nasty condition, or make the try block so short
you know exactly what he problem is.

Recall you can nest try blocks and rethrow exceptions you are not
prepared to deal with yet.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

andreas kinell - 31 Oct 2005 18:08 GMT
>> > Hi,
>> >
[quoted text clipped - 27 lines]
> Like i said I don't want to deal with the exception in the catch claus.
> Doing so, will not return me to where I left off.

That's what you have the finally clause for.

try {
   // code that could throw an exception
} catch (Exception e) {
   // deal with the exception
} finally{
   // resume execution here in _any_ case (regardless of exception thrown
or not
}

andreas
Sharp Tool - 01 Nov 2005 09:33 GMT
> >> > Hi,
> >> >
[quoted text clipped - 40 lines]
>
> andreas

Sure. I think the take home message here is that you cannot test directly if
an exception will be thrown. Rather you should test indirectly if an
exception will not be thrown and if it returns false then you know it will -
if you negate the negation then you back to square one.

Sharp Tool
andreas kinell - 02 Nov 2005 22:24 GMT
>> >> > Hi,
>> >> >
[quoted text clipped - 50 lines]
> will -
> if you negate the negation then you back to square one.

I rather think the take home message is:
you should not try to work around exception handling, because it was written
with the intention to make someone handle it.
On one side we have a good exception concept and on the other side we try to
avoid using it. Doesn't make too much sense.

andreas
Tor Iver Wilhelmsen - 30 Oct 2005 20:03 GMT
> I want to check if a statement throws an exception.
> If it does, I want to deal with it then and there, and not at the catch
> claus.

Catch clauses are the only places* you deal with exceptions. Do you
find the syntax too verbose or something?

try {
  str.charAt(0);
}
catch (ArrayIndexOutOfBoundsException e) {
  System.out.println("It would have been cheaper to test str.length()");
}

You don't need to have more than a single statement in the try block,
you know.

*) Plus in Thread.UncaughtExceptionHandler.uncaughtException() which
  nobody uses.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.