Java native exception handling

Java native exception handling

When an exception is thrown in Java, the Java Virtual Machine automatically looks for the nearest enclosing exception handler and unwinds the stack if necessary. This style of exception handling frees the programmer from caring about and handling unusual error cases at every operation in the program. Instead, the Java Virtual Machine propagates the error conditions automatically to a location (the catch clause in Java) that can handle the same class of error conditions in a centralized way.

While other languages such as C++ support a similar notion of exception handling, there is no uniform and general way to throw and catch exceptions in native languages. The JNI therefore requires you to check for possible exceptions after calling JNI functions. The JNI also provides functions that allow your native methods to throw Java exceptions. These exceptions can then be handled either by other parts of your native code or by the Java Virtual Machine. After the native code catches and handles an exception, it can either clear the pending exception so that the computation may continue, or it can throw another exception for an outer exception handler.

Many JNI functions may cause an exception to be thrown. For example, the GetFieldID function described in the previous section throws a NoSuchFieldError if the specified field does not exist. To simplify error checking, most JNI functions use a combination of error codes and Java exceptions to report error conditions. For example, you may check if the jfieldID returned from GetFieldID is zero (0) instead of calling the JNI function ExceptionOccurred . When the result of GetFieldID is not zero (0), you are guaranteed that there is no pending exception.

The remainder of this section illustrates how to catch and throw exceptions in native code. The example code is in CatchThrow.java .

The CatchThrow.main method calls the native method. The native method, defined in CatchThrow.c , first invokes the Java method CatchThrow.callback , as follows:

. jclass cls = (*env)->GetObjectClass(env, obj); jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V"); jthrowable exc; if (mid == 0) < return; >(*env)->CallVoidMethod(env, obj, mid); .

Note: Because CallVoidMethod throws a NullPointerException , the native code can detect this exception after CallVoidMethod returns by calling the method ExceptionOccurred :

. exc = (*env)->ExceptionOccurred(env); if (exc) < .

You can see that it is a simple matter to catch and handle an exception. In our example, we do not do much about the exception in CatchThrow.c except to use the method ExceptionDescribe to output a debugging message. The native method then throws an IllegalArgumentException . It is this IllegalArgumentException that the Java code which invoked the native method will see.

. (*env)->ExceptionDescribe(env); (*env)->ExceptionClear(env); newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); if (newExcCls == 0) < /* Unable to find the new exception class, give up. */ return; >(*env)->ThrowNew(env, newExcCls, "thrown from C code"); .

The ThrowNew function constructs an exception object from the given exception class and message string and posts the exception in the current thread.

Note: It is extremely important to check, handle, and clear the pending exception before calling subsequent JNI functions. Calling arbitrary JNI functions with a pending exception may lead to unexpected results. You can safely call only a small number of JNI functions when there is a pending exception. These functions are ExceptionOccurred , ExceptionDescribe , and ExceptionClear .

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.

Читайте также:  Str to datetime php

Источник

Java native exception handling

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью