NetBeans is like the long lost love that has aged with
grace and dignity, retaining the characteristics that made you love her in the
first place, whereas Eclipse is like the aging, overeating trophy wife who
thinks she’s still “all that” and lets you know about it, frequently.
Friday, December 9, 2011
Sunday, November 27, 2011
NullPointerException
package
org.training.exceptions;
public class NullPointer {
public static void main(String[]
args) {
new NullPointer();
}
public NullPointer(){
Object[]
object = {12,25,68,45};
try {
for (Object o : object ) {
System.out.println(o);
}
//
explicitly dereferencing object array
object = null;
for (Object o : object ) {
System.out.println(o);
}
}catch(NullPointerException
nfe) {
nfe.printStackTrace();
}
}
}
Output:
12
25
68
45
java.lang.NullPointerException
at
org.training.exceptions.NullPointer.<init>(NullPointer.java:21)
at
org.training.exceptions.NullPointer.main(NullPointer.java:7)
ClassNotFoundException
ClassNotFoundException is one of the most commonly occurring
exceptions. This exception is thrown when the executing program is unable to
find the defined class. This could be because of various reasons, wrong
classpath, older version of jar file etc...
package
org.training.exceptions;
public class ClassNotFound {
public static void main(String[]
args) {
new ClassNotFound();
}
public
ClassNotFound(){
try {
// load the
java.lang.Strung class,
// this would thrown an ClassNotFoundException
because there is no class called “Strung”
defined in the java.lang package
Class c =
Class.forName("java.lang.Strung");
}catch(ClassNotFoundException
cnfe) {
cnfe.printStackTrace();
}
}
}
Output:
java.lang.ClassNotFoundException: java.lang.Strung
at
java.net.URLClassLoader$1.run(Unknown Source)
at
java.security.AccessController.doPrivileged(Native Method)
at
java.net.URLClassLoader.findClass(Unknown Source)
at
java.lang.ClassLoader.loadClass(Unknown Source)
at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at
java.lang.ClassLoader.loadClass(Unknown Source)
at
java.lang.Class.forName0(Native Method)
at
java.lang.Class.forName(Unknown Source)
at
org.training.exceptions.ClassNotFound.<init>(ClassNotFound.java:14)
at
org.training.exceptions.ClassNotFound.main(ClassNotFound.java:7)
NoSuchMethodException
package
org.training.exceptions;
import
java.lang.reflect.Method;
public class NoSuchMethod {
public static void main(String[]
args) {
new NoSuchMethod();
}
public NoSuchMethod(){
try {
// load the java.lang.String class, if not found throws ClassNotFoundException
// load the java.lang.String class, if not found throws ClassNotFoundException
Class
c = Class.forName("java.lang.String");
try {
Class[]
arguments = new Class[2];
// search for method called
"someMethod" with two parameters
// since there
is no method called "someMethod(arg1, arg2)", in the java.lang.String
class, this throws an NoSuchMethodException
Method
m = c.getDeclaredMethod("someMethod", arguments);
}catch(NoSuchMethodException
nsme) {
nsme.printStackTrace();
}
}catch(ClassNotFoundException
cnfe) {
cnfe.printStackTrace();
}
}
}
Output:
java.lang.NoSuchMethodException:
java.lang.String.someMethod(null, null)
at
java.lang.Class.getDeclaredMethod(Unknown Source)
at
org.training.exceptions.NoSuchMethod.<init>(NoSuchMethod.java:19)
at
org.training.exceptions.NoSuchMethod.main(NoSuchMethod.java:9)
Friday, November 25, 2011
NumberFormatException
package
org.training.exceptions;
public class NumberFormat {
public static void main(String[]
args) {
String
string = "hello";
try {
// this
parsing doesn't work as hello is not a number
Integer
integer = Integer.parseInt(string);
System.out.println(integer++);
}catch(NumberFormatException
nfe) {
System.out.println("Number
Format Exception caught, check what you are parsing");
nfe.printStackTrace();
}
}
Output:
Number Format Exception caught,
check what you are parsing
java.lang.NumberFormatException: For input
string: "hello"
at
java.lang.NumberFormatException.forInputString(Unknown Source)
at
java.lang.Integer.parseInt(Unknown Source)
at
java.lang.Integer.parseInt(Unknown Source)
at
org.training.exceptions.NumberFormat.main(NumberFormat.java:11)
NegativeArraySizeException
package
org.training.exceptions;
public class
NegativeArraySize {
public static void main(String[]
args) {
int array[] =
{1,2,3,4,5};
try {
array
= new int[-8];
}catch(NegativeArraySizeException
nase) {
System.err.println("Negative
Array Size Exception has occured, check array initialization again. ");
}
for (int i: array) {
System.out.println(i);
}
}
}
Output:
Negative Array Size Exception has occurred, check array initialization again.
1
2
3
4
5
Subscribe to:
Comments (Atom)