Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

Exception Handling

• When we write any Java program then two terms will come into the picture first one is Error and Second one is Exception.

1. Error: In Java we have 2 different types of Error.

a. Compile Time Error.
b. Runtime Error.

a. Compile Time Error:

• In the case of compile time error, if any syntactical mistakes are there, then it is called as compile-time error which will be identified at the time of compilation.
• Compile-time errors show that the compiler is inefficient to compile that specific program without correcting that.

            
class hello{
    int a=10;
    public static void main(String ags[]){
    system.out.println(a);
    }	
}
    test.java:4: error: non-static variable a cannot be referenced from a static context
        system.out.println(a);
                           ^
    test.java:4: error: package system does not exist
        system.out.println(a);
              ^
            

In this example, ‘s’ is in lowercase but it must be in uppercase because in java.lang.System class ‘s’ is in uppercase so it will get a compilation error ‘package system does not exist’. It must be like this:- System.out.println(a);

b. Runtime Error:

• The error which is being identified at runtime with the help of JVM is called runtime error.
• Runtime Error generally occurs due to the lack of resource.

Example: Unviability of main method in class and we are trying to runcorresponding .class file then we will get the error at the run time.
If any stack memory related problem is identified, that will be represented by java.lang.StackOverflowError (memory required by the JVM to process).

2. Exception: Some unexpected operation which occurs is a program is called exception.
In Java every exception is a class and bellow is the API associated with the exception.

In Java there are 2 different types of Exception:

a. Compile-Time Exception (Checked Exception)
b. Runtime Exception (Unchecked Exception)

a. Compile-Time Exception (Checked Exception) :

• As we know in Java every exception is a class. When an exception is not a sub class of java.lang.RuntimeException class is called Compile-Time Exception or Checked Exception.
When a Checked or Compile-Time exception occurs into Java program then we must have to handle that exception using try-catch block or using throws keyword .

            
class JTC{
		public static void main(String arg[]){
			Class.forName("A");
		}
}
class A{
		static{
			System.out.println("A class is loading by forName()");
		}
}
    test.java:3: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
            Class.forName("A");
                         ^
            

As we know forName() method is available in java.lang.Class which we use to load a class dynamically and it throws java.lang.ClassNotFoundException when the specified .class file is not available on the location.
In the above exception we are using forName() method to load A.class file and because forName() method throws java.lang.ClassNotFoundException which is a Checked Exception and we are not handling, that is the reason unreported exception error is occurring at the time of compilation.
In the bellow example we are using theforName() to load A.class file again but along with try-catch block.

            
class JTC{
	public static void main(String arg[]){
		try{
			Class.forName("A");
		}catch(ClassNotFoundException e){
			System.out.println(e);
		}
	}
}
class A{
	static{
		System.out.println("A class is loading by forName()");
	}
}
    A class is loading by forName()
            

b. Runtime Exception (Unchecked Exception) :

• In Java when an exception is a direct or indirect sub class of java.lang.RuntimeException is called Runtime Exception.
• When we have any provision of Runtime Exception in Java Program then handle the exception is not mandatory, if we are handling the exception it will work otherwise JVM automatically handle the exception.

            
class JTC{
	public static void main(String arg[]){
		int a = 10;
		int b = 0;
		int c = a / b; // Line-5
		System.out.println("c :- "+c);
	}
}

    Exception in thread "main" java.lang.ArithmeticException: / by zero
        at JTC.main(test.java:5)
            

In the above example in as we can see in Line-5 we get Arithmetic Exception because are dividing 10 by 0 and we are not handling the exception but because java.lang.ArithmeticException is a Runtime Exception so JVM automatically handle that exception automatically we will not get any Unreported Exception error at the time of compilation.