Exception handling – try catch

One of the ways of handling exception in java is by using Try-catch block.

Syntax for using try & catch

  1. try{
  2.     Normal statement(s)
  3. }
  4. catch (exceptiontype name){
  5.     Exception handling statement(s)
  6. }
try{
    Normal statement(s)
}
catch (exceptiontype name){
	Exception handling statement(s)
}


We place normal code that might throw an exception in Try block and Exception handling code in Catch block

Example :

Consider below example

  1. package com.kb.state;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ExceptionTryCatchExample {  
  6.        public static void main(String args[])
  7.        {
  8.     int dividend = 8;
  9.     int divisor = 0;
  10.     float result = 0;
  11.     result = dividend/divisor;
  12.     System.out.println("Result is " +result);
  13.        }
  14.     }
package com.kb.state;

import java.util.Scanner;

public class ExceptionTryCatchExample {  
	   public static void main(String args[])
	   {
	int dividend = 8;
	int divisor = 0;
	float result = 0;
	result = dividend/divisor;
	System.out.println("Result is " +result);
	   }
	}


The above program throws an exception and we need to handle it

We know that the line causing the exception is

  1.  result = dividend/divisor;
 result = dividend/divisor;


We need to place such lines in a Try-catch block

Let’s handle this exception using Try-catch block as below

  1. package com.kb.state;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ExceptionTryCatchExample {  
  6.        public static void main(String args[])
  7.        {
  8.     int dividend = 8;
  9.     int divisor = 0;
  10.     float result = 0;
  11.    
  12.         try {
  13.             result = dividend/divisor;
  14.             System.out.println("Result is " +result);
  15.         } catch (Exception e) {
  16.             System.out.println("Can not perform dvision as Divisor is Zero");
  17.         }
  18.        
  19.         System.out.println("End of Program");
  20.        
  21.        }
  22.     }
package com.kb.state;

import java.util.Scanner;

public class ExceptionTryCatchExample {  
	   public static void main(String args[])
	   {
	int dividend = 8;
	int divisor = 0;
	float result = 0;
	
		try {
			result = dividend/divisor;
			System.out.println("Result is " +result);
		} catch (Exception e) {
			System.out.println("Can not perform dvision as Divisor is Zero");
		}
		
		System.out.println("End of Program");
		
	   }
	}



We can see that because of exception handling using Try-catch, our program is continuing further execution even after the exception.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on