Suppose you have a Java class which defines a copyFile(String, String)
method:
public class FileSystem {
public static void copyFile(String sourcePath, String destinationPath)
throws IOException
{
// ignore the fact that I'm not properly managing resources...
FileInputStream source = new FileInputStream(sourcePath);
FileOutputStream destination = new FileOutputStream(destinationPath);
copyStream(source, destination);
source.close();
destination.close();
}
private static void copyStream(InputStream source, OutputStream destination)
throws IOException
{
byte[] buffer = new byte[1024];
int length;
while ( (length = source.read(buffer)) != -1 ) {
destination.write(buffer, 0, length);
}
}
}
And suppose you wrap this in a Java stored procedure:
CREATE PROCEDURE copy_file (source_path VARCHAR2, destination_path VARCHAR2)
AS LANGUAGE JAVA
NAME 'FileSystem.copyFile(String, String)';
Now suppose you call the copy_file
stored procedure and a Java IOException
is thrown:
BEGIN
copy_file( '/some/file/that/does/not/exist.txt', '/path/to/destination.txt' );
END;
In the PL/SQL block, the error raised is:
ORA-29532 Java call terminated by uncaught Java exception
The error message also contains a description of the uncaught Exception
, but it is still an ORA-29532
. Is there a way to throw an Exception
from Java that dictates both the error code and the error message raised in PL/SQL?
As I know, Oracle can't catch the Java errors directly. And the Oracle docs note of the ORA-29532 validate this idea:
According to this text, I think you should handle the exception in the Java code.
You can solve that with these things:
System.out.println();
.Edit: Adam's final solution
This is roughly what I implemented based on the answer:
Then in PL/SQL: