Java Program on Exception Handling

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

package beans;

import java.util.Scanner;

public class TestMain 
{
    public static void main(String[] args) 
    {
           int a,b,c;
           Scanner scan=new Scanner(System.in);
           System.out.println("Enter First Number");
           a=scan.nextInt();
           System.out.println("Enter Second Number");
           b=scan.nextInt();
         try
         {    
          
          c=a/b; // throw an object of ArithmeticException class
           System.out.println("Division is " + c);
         }   
         catch(ArithmeticException e)
         {
           e.printStackTrace();
         } 
          
          
           c=a+b;
           System.out.println("Addition is " + c);
          c=a-b;
          System.out.println("subtraction is " + c); 
          c=a*b;
          System.out.println("Multiplication is " + c); 
         System.out.println("Prgram Ends here.............."); 
    }
    
}

Program 2

package beans;

public class TestMain2 
{
     public static void main(String[] args) 
     {
////          int ar[]={10,20,30,40,50};
////          System.out.println(ar[9]);
//           String str="Data Flair Indore";
//           System.out.println(str.charAt(34));
            String S1="Hello",S2="5";   
            int a,b,c;
            a=Integer.parseInt(S1);
            b=Integer.parseInt(S1);
            c=a+b;
            System.out.println(c);
            
            
          
     }
     
}

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses
Image

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *