/* * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// This file provides the basic support for exception handling in the VM. // Note: We do not use C++ exceptions to avoid compiler dependencies and // unpredictable performance. // // Scheme: Exceptions are stored with the thread. There is never more // than one pending exception per thread. All functions that can throw // an exception carry a THREAD argument (usually the last argument and // declared with the TRAPS macro). Throwing an exception means setting // a pending exception in the thread. Upon return from a function that // can throw an exception, we must check if an exception is pending. // The CHECK macros do this in a convenient way. Carrying around the // thread provides also convenient access to it (e.g. for Handle // creation, w/o the need for recomputation).
// Forward declarations to be independent of the include structure.
class JavaThread; class Handle; class Symbol; class JavaCallArguments; class methodHandle;
// The ThreadShadow class is a helper class to access the _pending_exception // field of the Thread class w/o having access to the Thread's interface (for // include hierarchy reasons).
class ThreadShadow: public CHeapObj<mtThread> { friendclass VMStructs; friendclass JVMCIVMStructs;
protected:
oop _pending_exception; // Thread has gc actions. constchar* _exception_file; // file information for exception (debugging only) int _exception_line; // line information for exception (debugging only) friendvoid check_ThreadShadow(); // checks _pending_exception offset
// The following virtual exists only to force creation of a vtable. // We need ThreadShadow to have a vtable, even in product builds, // so that its layout will start at an offset of zero relative to Thread. // Some C++ compilers are so "clever" that they put the ThreadShadow // base class at offset 4 in Thread (after Thread's vtable), if they // notice that Thread has a vtable but ThreadShadow does not. virtualvoid unused_initial_virtual() { }
// Exceptions is a helper class that encapsulates all operations // that require access to the thread interface and which are // relatively rare. The Exceptions operations should only be // used directly if the macros below are insufficient.
class Exceptions { staticbool special_exception(JavaThread* thread, constchar* file, int line, Handle exception); staticbool special_exception(JavaThread* thread, constchar* file, int line, Symbol* name, constchar* message);
// Count out of memory errors that are interesting in error diagnosis staticvolatileint _out_of_memory_error_java_heap_errors; staticvolatileint _out_of_memory_error_metaspace_errors; staticvolatileint _out_of_memory_error_class_metaspace_errors;
// Count linkage errors staticvolatileint _linkage_errors; public: // this enum is defined to indicate whether it is safe to // ignore the encoding scheme of the original message string. typedefenum {
safe_to_utf8 = 0,
unsafe_to_utf8 = 1
} ExceptionMsgToUtf8Mode; // Throw exceptions: w/o message, w/ message & with formatted message. staticvoid _throw_oop(JavaThread* thread, constchar* file, int line, oop exception); staticvoid _throw(JavaThread* thread, constchar* file, int line, Handle exception, constchar* msg = NULL);
// There is no THROW... macro for this method. Caller should remember // to do a return after calling it. staticvoid fthrow(JavaThread* thread, constchar* file, int line, Symbol* name, constchar* format, ...) ATTRIBUTE_PRINTF(5, 6);
// Create and initialize a new exception static Handle new_exception(JavaThread* thread, Symbol* name,
Symbol* signature, JavaCallArguments* args,
Handle loader, Handle protection_domain);
// Exception counting for error files of interesting exceptions that may have // caused a problem for the jvm staticvolatileint _stack_overflow_errors;
// for logging exceptions staticvoid log_exception(Handle exception, constchar* message);
};
// The THREAD & TRAPS macros facilitate the declaration of functions that throw exceptions. // Convention: Use the TRAPS macro as the last argument of such a function; e.g.: // // int this_function_may_trap(int x, float y, TRAPS)
// The CHECK... macros should be used to pass along a THREAD reference and to check for pending // exceptions. In special situations it is necessary to handle pending exceptions explicitly, // in these cases the PENDING_EXCEPTION helper macros should be used. // // Macro naming conventions: Macros that end with _ require a result value to be returned. They // are for functions with non-void result type. The result value is usually ignored because of // the exception and is only needed for syntactic correctness. The _0 ending is a shortcut for // _(0) since this is a frequent case. Example: // // int result = this_function_may_trap(x_arg, y_arg, CHECK_0); // // CAUTION: make sure that the function call using a CHECK macro is not the only statement of a // conditional branch w/o enclosing {} braces, since the CHECK macros expand into several state- // ments! Also make sure it is not used on a function call that is part of a return statement!
// CAUTION: These macros clears all exceptions including async exceptions, use it with caution. #define CHECK_AND_CLEAR THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; return; } (void)(0 #define CHECK_AND_CLEAR_(result) THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; return result; } (void)(0 #define CHECK_AND_CLEAR_0 CHECK_AND_CLEAR_(0) #define CHECK_AND_CLEAR_NH CHECK_AND_CLEAR_(Handle()) #define CHECK_AND_CLEAR_NULL CHECK_AND_CLEAR_(NULL) #define CHECK_AND_CLEAR_false CHECK_AND_CLEAR_(false)
// CAUTION: These macros clears all exceptions except probable async exceptions j.l.InternalError. // So use it with caution. #define CLEAR_PENDING_NONASYNC_EXCEPTION (((ThreadShadow*)THREAD)->clear_pending_nonasync_exception()) #define CHECK_AND_CLEAR_NONASYNC THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_NONASYNC_EXCEPTION; return; } (void)(0 #define CHECK_AND_CLEAR_NONASYNC_(result) THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_NONASYNC_EXCEPTION; return result; } (void)(0 #define CHECK_AND_CLEAR_NONASYNC_0 CHECK_AND_CLEAR_NONASYNC_(0) #define CHECK_AND_CLEAR_NONASYNC_NH CHECK_AND_CLEAR_NONASYNC_(Handle()) #define CHECK_AND_CLEAR_NONASYNC_NULL CHECK_AND_CLEAR_NONASYNC_(NULL) #define CHECK_AND_CLEAR_NONASYNC_false CHECK_AND_CLEAR_NONASYNC_(false)
// The THROW... macros should be used to throw an exception. They require a THREAD variable to be // visible within the scope containing the THROW. Usually this is achieved by declaring the function // with a TRAPS argument.
// The CATCH macro checks that no exception has been thrown by a function; it is used at // call sites about which is statically known that the callee cannot throw an exception // even though it is declared with TRAPS.
// Use an EXCEPTION_MARK for 'local' exceptions. EXCEPTION_MARK makes sure that no // pending exception exists upon entering its scope and tests that no pending exception // exists when leaving the scope.
// See also preserveException.hpp for PreserveExceptionMark // which preserves pre-existing exceptions and does not allow new // exceptions.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.