• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include "tensorflow/c/c_api.h"
21 #include "tensorflow/java/src/main/native/exception_jni.h"
22 
23 const char kIllegalArgumentException[] = "java/lang/IllegalArgumentException";
24 const char kIllegalStateException[] = "java/lang/IllegalStateException";
25 const char kNullPointerException[] = "java/lang/NullPointerException";
26 const char kIndexOutOfBoundsException[] = "java/lang/IndexOutOfBoundsException";
27 const char kUnsupportedOperationException[] =
28     "java/lang/UnsupportedOperationException";
29 
throwException(JNIEnv * env,const char * clazz,const char * fmt,...)30 void throwException(JNIEnv* env, const char* clazz, const char* fmt, ...) {
31   va_list args;
32   va_start(args, fmt);
33   // Using vsnprintf() instead of vasprintf() because the latter doesn't seem to
34   // be easily available on Windows.
35   const size_t max_msg_len = 512;
36   char* message = static_cast<char*>(malloc(max_msg_len));
37   if (vsnprintf(message, max_msg_len, fmt, args) >= 0) {
38     env->ThrowNew(env->FindClass(clazz), message);
39   } else {
40     env->ThrowNew(env->FindClass(clazz), "");
41   }
42   free(message);
43   va_end(args);
44 }
45 
46 namespace {
47 // Map TF_Codes to unchecked exceptions.
exceptionClassName(TF_Code code)48 const char* exceptionClassName(TF_Code code) {
49   switch (code) {
50     case TF_OK:
51       return nullptr;
52     case TF_INVALID_ARGUMENT:
53       return kIllegalArgumentException;
54     case TF_UNAUTHENTICATED:
55     case TF_PERMISSION_DENIED:
56       return "java/lang/SecurityException";
57     case TF_RESOURCE_EXHAUSTED:
58     case TF_FAILED_PRECONDITION:
59       return kIllegalStateException;
60     case TF_OUT_OF_RANGE:
61       return kIndexOutOfBoundsException;
62     case TF_UNIMPLEMENTED:
63       return kUnsupportedOperationException;
64     default:
65       return "org/tensorflow/TensorFlowException";
66   }
67 }
68 }  // namespace
69 
throwExceptionIfNotOK(JNIEnv * env,const TF_Status * status)70 bool throwExceptionIfNotOK(JNIEnv* env, const TF_Status* status) {
71   const char* clazz = exceptionClassName(TF_GetCode(status));
72   if (clazz == nullptr) return true;
73   env->ThrowNew(env->FindClass(clazz), TF_Message(status));
74   return false;
75 }
76