Can anyone help to solve that confusion about exception? [duplicate] - java

This question already has answers here:
Understanding checked vs unchecked exceptions in Java
(21 answers)
Closed last month.
I am aware of run-time unchecked exceptions, but not checked exceptions. According to a video I found, checked and unchecked exceptions only happen at run time. I also discovered a page that claims checked exceptions happen during compilation. I used the Java 8 documentation, but I was unable to determine whether the checked exception happens at compile time or at run time. If you read anything in the Java documentation, do provide me with a reference.
If you read anything in the Java documentation, do provide me with a reference.

Checked Exceptions are to be handled by us by throwing a exception, While unchecked exceptions are handled by the compiler. To get some clarity regarding exceptions .Go through this documentation once.
https://www.baeldung.com/java-exceptions

Related

Android custom lint rule for handling java checked exception in kotlin

Since kotlin doesn't have the concept of the checked exception, many times we tend to forget about handling the exception while calling the java function(from kotlin) which throws checked exception and it would just crash in case of exception. Since this is the pretty common thing.
Do we have any android custom lint which would just remind us to handle the exception?

Exceptions package location in Java

Why some exceptions are located in their respective packages (like IOException, located in java.io) but others are in java.lang (like ArrayIndexOutOfBounds in java.lang). Is this because one is checked and other is Unchecked Exception?
I really don't think that base on checked and unchecked conditions java exceptions are located in respective packages.If we consider about java.io package and java.lang packages,
java.io package contains classes for system input and output through data streams, serialization and the file system.And also handling files also done by Java I/O API.
java.lan package contains all the classes that are using to the design of the Java programming language such as String, Math. And provide basic runtime support for threads and processes.
Java IOException is thrown whenever an input or output stream is interrupted.So this regarding System input and output.So this IOException class must come under java.io package.
Java RuntimeExceptions such as ArrayIndexOutOfBoundEceptions are checked at the runtime.java.lan package support to handle runtime threads.It's include classes that support runtime for threads and process.
So IOException,ArrayIndexOutOfBound are classes with relevant function.So those classes must be categorized under relevant packages.
First: ArrayIndexOutOfBoundsException is not inside java.lang.Throwable. It is inside the package java.lang. It is a subclass of java.lang.Throwable, but so are all exceptions (including IOException).
ArrayIndexOutOfBoundsException is inside java.lang because all the "basic stuff related to the core language" is in java.lang, and ArrayIndexOutOfBoundsException is "basic stuff related to the core language".
IOException is inside java.io because all the stuff related to I/O is in java.io, and IOException is "stuff related to I/O".
There is no deep or interesting technical reason.
As a general rule, it is a good idea to place checked exceptions near the code which is throwing those types of exceptions. This explains your first observation. On the other hand, the exception ArrayIndexOutOfBounds is an unchecked runtime exception. It is associated with a type of runtime error from which the program is generally not expected to recover. As such, it placed in java.lang.throwable with other similar runtime exceptions. So as a general rule, if you define custom exceptions for your program, you should therefore probably put them in the packages, or near the code, which uses them.

How to find all locations in a Java project where a RuntimeException is possible to be thrown?

I have a java project and I want a list of locations in source code where a RuntimeException might be thrown. Is there any tool or Eclipse plugin to do this?
With FindBugs or PMD the NullPointerExceptions might be located, but I need to locate others as well.
NullPointerException, ClassCastException and ArithmeticException are a special cases as they do not need to be explicitly thrown. However, everything else has to be thrown explicitly.
Searching for possible causes of NullPointerException is useful as its a common bug, however like all RuntimeException they are best avoided rather than handled. Searching for all possible causes is likely to give you a very large result which is unlikely to be useful. (I have been through this exercise myself and after lots of work didn't achieve much IMHO)
You are better off using code coverage and try edge case values to ensure you have good test cases which are likely to trigger all possible exception instead.
If you're only checking your own code, you can do this with Checkstyle. Look at the Coding Problems, Illegal Throws check.
You can specify any number of Exceptions, by default it is: java.lang.Throwable, java.lang.Error, java.lang.RuntimeException.
Note: This doesn't pick up subclasses of the above exceptions, it only picks up the classes themselves. It also does not check source code to which you don't have access, so runtime libraries won't be checked, for instance.
I do agree with the other answers. Since I don't know what you want to achieve, I want to offer a more direct answer to your question:
Unfortunately I know no tool that does this. Sorry. Depending on how important it is to you, you could invest some work yourself.
Check your code 'by hand'.
After that the JDK remains (and most likely some libs you are using). You can download the JDKs source code from Oracle. Than you can do a full text search for the occurrences of newly created exception instances again. Note, however, that even this tedious work will not give you all possible sources since some methods are missing there.

Getting Eclipse to trap on exceptions thrown only from my own code?

I'm using the most up-to-date version of Eclipse (Helios) for Java development. I've written a lot of code for my project, and I'm also using some 3rd-party code in the project.
It's normal for the 3rd-party code to internally throw exceptions, even when nothing is deeply wrong. It will catch these itself. During a normal run, the 3rd-party code might throw a lot of these not-really-a-problem Exceptions.
I'd like to tell Eclipse that, during debugging, it should break when any of my code throws an Exception, but not when other code I'm linking to throws an Exception. Does anyone know if Eclipse supports this?
I know Eclipse lets you break only when Exceptions of certain types are thrown, but that doesn't help when 3rd party code and my own both throw standard Exceptions.
AFAIK no. But you can set a root Exception and make all your exceptions extends it. Then you can set up a Exception Breakpoint on you root exception.
In the breakpoint window you can do so, there is an icon.

Differences between NoClassDefFoundError and ClassNotFoundException?

NoClassDefFoundError extends LinkageError which in turns extends Error.
Javadoc for Error class states:
An Error is a subclass of Throwable
that indicates serious problems that a reasonable application
should not try to catch.
Class loading methods like Class.forName() only declares ClassNotFoundException in throws clause. Which, in addition to above description of Error means that we should not be usually catching the NoClassDefFoundError when loading classes with Class.forName() etc.
My question is what are the conditions in which NoClassDefFoundError is thrown instead of ClassNotFoundException?
ClassNotFoundException is more likely to be thrown (to your code) in situations where you're manually loading classes - precisely for things like Class.forName(). These names may come from user input, for example.
NoClassDefFoundError will occur when a class file itself refers to a class that then can't be found. The class was present at some time, but now isn't - this isn't just a bug in the code that's trying to do reflection, it's a deployment mistake of not making all the required classes available. As far as I can tell a NoClassDefFoundError will usually or possibly always wrap a ClassNotFoundException - but the point is that this isn't something your code is meant to guard against, as it indicates an environment which is probably too broken to recover from.
At least, that's my understanding :)
NoClassDefFoundError occures at runtime because compiler not able to find .class file.

Categories