Top 10 Exception in Java

checked and unchecked exceptions is that checked exceptions are forced by compiler and used to indicate exceptional conditions that are out of the control of the program (for example, I/O errors), while unchecked exceptions are occurred during runtime and used to indicate ...

whenever we are trying to access Array Element out of range

public static void main(String[] args) {
int [] array = new int [10];
System.out.println(array[0]);
System.out.println(array[100]);

}
output
0
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 100
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:28)

String action = null;
System.out.println(action);
System.out.println(action.length());
output

null
Exception in thread “main” java.lang.NullPointerException
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:26)

public class ArraysDS {
public void m1() {
System.out.println(“m1”);
m2();}
public void m2() {
System.out.println(“m2”);
m1();}
public static void main(String[] args) {
ArraysDS a = new ArraysDS();
a.m1();
a.m2();
}
output
m1
m2
m1
m2
Exception in thread “main” java.lang.StackOverflowError
at java.io.FileOutputStream.write(FileOutputStream.java:326)

at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.PrintStream.write(PrintStream.java:482)

javac Desktop.java — compiling progaram
After Compilation ….

Java Desktop — Run program

if Desktop.class file not available then it will return Runtime Exception NoClassDefFoundError

String s = new String(“Baba”);
Object o = s;
System.out.println(o);

Object x = new Object();
String y = (String) x;
System.out.println(y);

output
Baba
Exception in thread “main” java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:29)

public class ArraysDS {
static int x = 10 / 0;
public static void main(String[] args) {
System.out.println(x);}}

Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at org.practice.datastructure.problem.ArraysDS.<clinit>(ArraysDS.java:12)

Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at org.practice.datastructure.problem.ArraysDS.<clinit>(ArraysDS.java:12)

public class ArraysDS {
public static void main(String[] args) {
Thread t1 = new Thread();
t1.setPriority(10);
t1.setPriority(100);}}

Exception in thread “main” java.lang.IllegalArgumentException
at java.lang.Thread.setPriority(Thread.java:1089)

at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:15)

public class ArraysDS {
public static void main(String[] args) {
int i = Integer.parseInt(“10”);
System.out.println(i);

int x = Integer.parseInt(“Ten”);
System.out.println(x);
}}
output
10
Exception in thread “main” java.lang.NumberFormatException: For input string: “Ten”
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:15)

public class Difference {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String S = in.next();
try {
int x = Integer.parseInt(S);
System.out.println(x);}
catch (NumberFormatException e) {
System.out.println(“Box”);}}}

HttpSession session = request.getSession();
System.out.println(session.getId()); — — — — — — — - o/p — — 121
session.invalidate();
System.out.println(session.getId()); — — — — o/p
IllegalStateException

public class ArraysDS {
public static void main(String[] args) {
Thread t1 = new Thread();
t1.start();
t1.start();
System.out.println();}}
o/p
Exception in thread “main” java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:16)

public class ArraysDS {
public static void main( String args[] ){
Scanner scanner = new Scanner( System.in );
System.out.print(“Enter ur age “);
int value = scanner.nextInt();
assert value>=18:” Not valid”;
System.out.println(“value is “+value);}}

Enter ur age 1
Exception in thread “main” java.lang.AssertionError: Not valid
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:21)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store