Beruflich Dokumente
Kultur Dokumente
try
{
//8o2ethng bad happen8 here
}
catch( NullPontercepton e )
{
Sy8te2.out.prntln( NullPontercepton +
e.toStrng() );
}
\hen the runtime system creates the exception
object, the reerence ,here named e`, contains
eerything that is known about the exception. By
using its toStrng() method, we reeal that
inormation.
]ara Copyright 2001-2004 1om lunter
Exception Handling
Aboe all, Lxception landling is an opportunity
to do something-it gies you the chance to
interene, but it is up to ,ov ,ov to plan what happens
when a speciic exception is thrown.
]ara Copyright 2001-2004 1om lunter
try
{
//8o2ethng bad happen8 here
}
catch( cepton e )
{
}
\hat hae we done here
Deeated 1he Lxception
landling Mechanism! 1he
program will resume executing as
i no exception occurred!
]ara Copyright 2001-2004 1om lunter
Exception Handling
In a console program-one that runs in the DOS
window or UNIX shell-an unhandled exception
results in the program terminating.
A GUI program can go on executing een ater an
unhandled exception-but producing incorrect and
unpredictable results in the process.
Moral o the Story
In a GUI program-be especially especially careul to
handle your errors intelligently.
]ara Copyright 2001-2004 1om lunter
1he
Causes
o
Lxceptions
]ara Copyright 2001-2004 1om lunter
The Causes oI Exceptions
An Lxception is 1hrown or One o 1hree Reasons:
1.-An abnormal execution condition was synchronously
detected by the Jaa Virtual Machine.
Possible Causes:
-Laluation o an expression iolates the rules o the
Jaa Language, such as an integer diide by zero, etc.
-An error occurs in loading or linking the Jaa program.
-Some limitation in a resource is exceeded, such as
using too much memory.
]ara Copyright 2001-2004 1om lunter
The Causes oI Exceptions
An Lxception is 1hrown or One o 1hree Reasons:
2.-A throw statement was executed in Jaa code
Possible Causes:
-1he program execution met an error that had been
expected.
]ara Copyright 2001-2004 1om lunter
The Causes oI Exceptions
An Lxception is 1hrown or One o 1hree Reasons:
3.-An asynchronous exception occurred.
Possible Causes:
-1he method 8top() o class %hread was inoked.
-An internal error has occurred in the irtual machine.
]ara Copyright 2001-2004 1om lunter
low to re re throw
our Own
Lxceptions
]ara Copyright 2001-2004 1om lunter
How to re re- -throw our Own Exceptions
Usually, the JVM decides when to throw an
exception.
But, the programmer hersel can choose when to
throw an exception.
Also, i your catch block handles an exception but
then decides it really can`t handle it decides it really can`t handle it, you can include
logic in your catch block to re re--throw the exception throw the exception to
a handler higher up in the call stack.
]ara Copyright 2001-2004 1om lunter
try
{
// So2e proce88ng hereno ecepton though
try
{
// a Doodlebugcepton occur8 here.
}
catch( Doodlebugcepton e )
{
// 8o2e ecepton proce88ng...
Sy8te2.out.prntln( Doodlebug ecepton );
throw new Bugcepton();
}
}
catch( Bugcepton e )
{
Sy8te2.out.prntln( Bugcepton! +
e.toStrng() );
}
lirstly, we are within the blue try block.
Ater some ok processing, we`re in the red try block.
\e`re able to do
logic in the catch
block, but or some
reason, the
programmer
thought it was
necessary to rethrow rethrow
the exception object
e`
Ater the rethrow, the exception is handled by the
nearest enclosing enclosing catch block that is able to handle it.
]ara Copyright 2001-2004 1om lunter
How to re re- -throw our Own Exceptions
A method is not permitted to throw an exception
o its own unless you speciy in the method signature
which speciic Lxceptions it throws.
]ara Copyright 2001-2004 1om lunter
Compile-1ime
Checking
o
Lxceptions
]ara Copyright 2001-2004 1om lunter
Compile-Time Checking oI Exceptions
I a method or constructor method
announces that it has the ability to throw a
certain type o exception, then the compiler
checks to see i there are exception handlers in
place to deal with those announced exceptions.
1he throws clause or the method must mention
the class o that exception, or one o the
Superclasses o the exception that the method can
throw.
]ara Copyright 2001-2004 1om lunter
Compile-Time Checking oI Exceptions
Lxceptions that are explicitly announced as
being possible by methods or constructors are
known as...
checked exceptions.`
]ara Copyright 2001-2004 1om lunter
Compile-Time Checking oI Exceptions
1he otber kinds o exceptions are known as
vvchecked exceptions.`
1hese come rom the class Runt2ecepton
and its subclasses, and rom the class rror and its
subclasses.
All other exception classes are checked exception
classes.
]ara Copyright 2001-2004 1om lunter
Compile-Time Checking oI Exceptions
\hy Lrrors are not checked:
rror and its subclasses are exempt
rom compile-time checking because they are
unpredictable.
1hey can occur anywhere so it would
needlessly clutter up your program i you
attempted to catch them.
Also, more importantly, no recoery is
eer possible rom an rror .
]ara Copyright 2001-2004 1om lunter
Compile-Time Checking oI Exceptions
\hy RuntimeLxceptions are not checked:
1hese problems are usually beyond the
programmer`s control. 1hus, it would be a
needless irritation to the programmer i she was
orced to catch these.
]ara Copyright 2001-2004 1om lunter
publc cla88 %e8tcepton
{
publc 8tatc vod 2an( Strng[] arg8 )
{
nt[] a = new nt[2];
a[0] = 1;
a[1] = 2;
a[3] = 3;
Sy8te2.out.prntln( Array a[2]= + a[2] );
}
}
As you can see, this 2an method
is ripe or an exception. And, as
expected, when we execute it, we
will see the ollowing:
]ara Copyright 2001-2004 1om lunter
1he JVM encountered an
uncaught exception.
1his was where the JVM was when it
met the uncaught exception.
]ara Copyright 2001-2004 1om lunter
publc 8tatc vod 2an( Strng[] arg8 )
{
nt[] a = new nt[2];
try
{
a[0] = 1;
a[1] = 2;
a[2] = 3;
Sy8te2.out.prntln( "Array a[2]=" + a[2] );
}
catch( ArrayIndeOutO1Bound8cepton e )
{
Sy8te2.out.prntln( "Do 8o2ethng here" + e );
}
}
1his is our array. It has
two slots aailable.
1his statement will attempt to use a
non-existent 3rd slot in our array.
]ara Copyright 2001-2004 1om lunter
]ara Copyright 2001-2004 1om lunter
Clicking on the e` exception
object, we see exactly the kind
that was thrown.
]ara Copyright 2001-2004 1om lunter
1he Stack Dump:
\hen our
catch
Misses
]ara Copyright 2001-2004 1om lunter
The Stack Dump
I an unexpected exception occurs, then your
careully-planned catch block will ail to capture the
exception.
In this case, your console will ill with a stack dump:
041.453 1024 HttpTransport X IJ.Error
java.net.SocketException: Connection reset by peer
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
java.io.IJException(java.lang.String)
java.net.SocketException(java.lang.String)
void java.net.SocketJutputStream.socketWrite(byte ,, int, int)
void java.net.SocketJutputStream.write(byte ,, int, int)
void com.ibm.servlet.engine.http_transport.HttpTransportConnection.write(byte ,, int, int)
void com.ibm.servlet.engine.http_transport.HttpTransportConnection.prepareForWrite(int, java.lang.String, java.lang.String ,)
void com.ibm.servlet.engine.srp.SRPConnection.prepareForWrite(int, java.lang.String, java.lang.String ,, java.lang.String ,)
void com.ibm.servlet.engine.srt.SRTServletResponse.commit()
void com.ibm.servlet.engine.srt.SRTServletResponse.alertFirstWrite()
void com.ibm.servlet.engine.srt.SRTJutputStream.write(byte ,, int, int)
void java.io.JutputStreamWriter.write(char ,, int, int)
void java.io.PrintWriter.write(char ,, int, int)
void com.ibm.servlet.engine.webapp.BufferedWriter.writeJut(char ,, int, int)
void com.ibm.servlet.engine.webapp.BufferedWriter.write(char ,, int, int)
void java.io.PrintWriter.write(char ,, int, int)
void com.sun.jsp.runtime.JspWriterImpl.flushBuffer()