Saturday, July 11, 2015

Interview - Java Exception



NoClassDefFoundError vs ClassNotFoundExcepiton
http://java67.blogspot.com/2012/12/noclassdeffounderror-vs-classnotfoundexception-java.html
NoClassDefFoundError indicates that class was present during time of compilation but not available when you run Java program, some time error on static initializer block can also result in NoClassDefFoundError.

On the other hand ClassNotFoundException is nothing to do with compile time, ClassNotFoundException comes when you try to load a class in runtime using Reflection, e.g. loading SQL drivers and corresponding Class loader is not able to find this class.

1) NoClassDefFoundError is an Error which is unchecked in nature, i.e. doesn't require try-catch or finally block. On the other hand ClassNotFoundException is a checked Exception and requires mandatory handing using either try with catch block or try with finally block, failure to do so will result in compile time error.

2) If you are experiencing NoClassDefFoundError in J2EE environment, there could be host of reason, one being multiple class loader and visibility of class among them.

3) Often java.lang.ClassNotFoundException is thrown as result of following method call, Class.forName(), ClassLoader.findSystemClass() and ClassLoader.loadClass().

4) Another difference between NoClassDefFoundError and ClassNotFoundException is that NoClassDefFoundError is a LinkageError and can come during linking, while java.lang.ClassNotFoundException is an Exception and occurs during runtime.


Comparison of Exception Handling in C++ and Java
http://massivetechinterview.blogspot.com/2014/07/comparison-of-exception-handling-in-c.html

3 ways to solve java.lang.NoClassDefFoundError in Java J2EE
http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError. It’s important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and name was provided during runtime not on compile time.

How to resolve java.lang.NoClassDefFoundError in Java
1) Class is not available in Java Classpath.
2) You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.
3) Any start-up script is overriding Classpath environment variable.

4) Because NoClassDefFoundError is a sub class of java.lang.LinkageError it can also come if one of it dependency like native library may not available.
4) Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to failure of static initialization is quite common.
5) If you are working in J2EE environment than visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError

Use System.getproperty("java.classpath")and it will print the classpath from there you can at least get an idea of your actual runtime classpath.

NoClassDefFoundError in Java due to Exception in Static Initializer block
when your class perform some static initialization in static block like many Singleton classes initialized itself on static block  to take advantage of thread-safety provided by JVM during class initialization process, and if static block throw an Exception, the class which is referring to this class will get NoclassDefFoundError in Java. If you look at your log file you should watch for any java.lang.ExceptionInInitializerError because that could trigger java.lang.NoClassDefFoundError: Could not initialize class on other places.

7) Permission issue on JAR file can also cause NoClassDefFoundError in Java. I
10) java.lang.NoClassDefFoundError can be caused due to multiple classloaders in J2EE environments.

12) Java program can also throw java.lang.NoClassDefFoundError during linking which occurs during class loading in Java. - class is not here at runtime.
http://www.corejavainterviewquestions.com/java-exception-interview-questions/
Exceptions are a way to programmatically convey system and programming errors. All exceptions inherit from Throwable.  When something goes wrong you can use the throw keyword to fire an exception.

There are 2 types of exception in Java, what are they and what’s the difference?
The two types of exception are checked and unchecked

A checked exception is one that forces you to catch it. It forms part of the API or contract. Anyone using code that throws a checked Exception can see that as it is declared on the method and they are forced to handle it using a try/catch block.  Unchecked on the other hand does not need to be caught and does not notify anyone using the code that it could be thrown.

All exceptions are checked exceptions except those that inherit from java.lang.RuntimeException.

When catching exceptions it is important to do so in order from most specific to least specific.  If your catch block catches Exception first and then IOException second, the first catch block will always catch any Exception coming through and the IOException will be rendered useless (and in fact it will cause a compiler error saying the Exception has already been caught)

C sharp does not have Checked Exceptions.  Can you tell me why this might be? Who do you think was right, Java or C sharp?

Difference between Error vs Exception in Java
http://java67.blogspot.com/2012/12/difference-between-error-vs-exception.html
Both Error and Exception are derived from java.lang.Throwable in Java but main difference between Error and Exception is kind of error they represent. java.lang.Error represent errors which are generally can not be handled and usually refer catastrophic failure.

Error are fatal in nature and recovery may not be possible, on the other hand by carefully handling Exception you can make your code more robust and guard against different scenarios.

Main difference on Error vs Exception is that Error is not meant to catch as even if you catch it you can not recover from it.
2) Error are often fatal in nature and recovery from Error is not possible which is different in case of Exception which may not be fatal in all cases.
3) Unlike Error, Exception is generally divided into two categories e.g. checked and unchecked Exceptions. Checked Exception has special place in Java programming language and require a mandatory try catch finally code block to handle it. On the other hand Unchecked Exception, which are subclass of RuntimeException mostly represent programming errors.
4) Similar to unchecked Exception, Error in Java are also unchecked. Compiler will not throw compile time error if it doesn't see Error handled with try catch or finally block. In fact handling Error is not a good Idea because recovery from Error is mostly not possible.

java.lang.UnsatisfiedLinkError: Library not found
http://java67.blogspot.com/2014/01/javalangunsatisfiedlinkerror-library-not-found-tibco-android.html
1. 90%: the library which you are using directly or indirectly is not in path
2. The library might not have right kind of permissions e.g. placed under a home directory of a user, which is not accessible.
java.lang.OutOfMemoroyError: Java heap space and java.lang.OutOfMemoryError: PermGen space 

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts