Exception handling

S

Sharp Tool

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
 
A

Andrew McDonagh

Sharp said:
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
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'
 
M

Malte

Sharp said:
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

Why not:

try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// deal with it, ie do something 1
}
 
S

Sharp Tool

Sharp said:
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

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.

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
 
S

Sharp Tool

Sharp said:
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

Why not:

try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// 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
 
T

Thomas Hawtin

Sharp said:
[Malte wrote:]
try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// 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.

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
 
R

Roedy Green

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.
 
T

Tor Iver Wilhelmsen

Sharp Tool said:
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.
 
A

andreas kinell

Sharp Tool said:
Sharp said:
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

Why not:

try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// 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.

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
 
S

Sharp Tool

Sharp Tool said:
Sharp Tool wrote:
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


Why not:

try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// 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.

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

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
 
A

andreas kinell

Sharp Tool said:
Sharp Tool said:
Sharp Tool wrote:
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


Why not:

try {
// whatever
// do something 2
} catch (ArrayIndexOutOfBoundsException e) {
// 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.

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

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top