Can c code raise java exceptions? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to throw exceptions in JNI code?
I see that the System#arraycopy could throw ArrayStoreException or IndexOutOfBoundsException etc.
But I also saw that System#arraycopy is a using a native method, which means C code, right?
So how is it possible that C code could throw any of these java exceptions?

There are no problems in throwing java exception from native code. You can do this easily with code like this one:
jclass cls = env->FindClass("java/lang/ArrayStoreException");
env->ThrowNew(cls, message);

Related

Why can't we just throw Exception/Throwable instead of having multiple exceptions [duplicate]

This question already has answers here:
When should we create our own Java exception classes? [closed]
(6 answers)
Closed 2 years ago.
This question may sound stupid, but why do we have to throw several exceptions while we can just throw Throwable or Exception?
As far as I know, this is similar to why, in a try-catch block, programmers like to list out the various exceptions in individual catch clauses rather than just catching Exception overall. This is mostly a readability and documentation best-practice. Anyone else reading your program will know explicitly what errors are being tracked for or thrown. If I'm reading dense code from someone else that is dealing with audio input-output, for example, it is much more informative to me if I know that I can expect a LineUnsupportedException rather than just a generic exception.

How can the JVM return an exception object at run time for a checked exception, if checked exceptions occur at compile time? [duplicate]

This question already has answers here:
Checked Exception is compile time or runtime? [closed]
(2 answers)
Closed 3 years ago.
I understand that checked exceptions in Java are handled at compile time. If this is the case, how can the JVM return a checked exception object at run-time if we are still compiling the source code?
Exceptions (checked or unchecked) occur at runtime. The only difference is that checked exceptions must be handled (by either adding them to the throws clause of the method or a try-catch block); the handled part there is static code analysis - which determines one (or both) of those two conditions is met at compile time.

How can we decide that custom exception should be a checked or unchecked exceptions [duplicate]

This question already has answers here:
How does one decide to create a checked excpetion or an unchecked exception [duplicate]
(2 answers)
Closed 4 years ago.
When defining a custom Exception in java, how can we decide that it should be a checked or unchecked exceptions
From https://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ which is actually quoting https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception

Error Casting in Java [duplicate]

This question already has answers here:
Converting a generic argument to an int in java, provided that it is a number
(4 answers)
Closed 8 years ago.
I have the next problem:
I am making a simple cast:
TargetBot2Params params = (TargetBot2Params)bot.getParams();
But I get the next error:
Inconvertible types
Requiered: TargetBot2Params
Found: UT2004BotParameters
And I dont know why occurs this, because I think that it doesnt do the cast.
Anyone knows why occurs this?
Thanks for your time.
The error message says it all.
For the casting to be successful, UT2004BotParameters has to either be a TargetBot2Params (i.e., extends TargetBot2Params) or, if TargetBot2Params is an interface, implement it.

Null Pointer Exception in Android , what does it say? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I have faced many time the NullPointerException in android. Some times I understand why the exception is thrown.
My Question is: Android applications are developed in Java and Java does not support pointers, then why is the exception called NullPointerException?
NullPointerException is a situation in code where you try to access/ modify an object which has not been initialized yet. It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘null’.
A NullPointerException is thrown when you use a null reference. The name is a holdover from an early prototype of Java.

Categories