简体   繁体   中英

How to handle exceptions in program

So I have this program here which I have to finish as an assignment. And before anyone asks, I have to include constructors, objects, member methods, a separate main class and OO concepts like inheritance, polymorphism and abstraction. I have done most of the programming and it works well. The one I can't get it to do is to handle exceptions when I enter a wrong input. Here's the code.

Main Class

import java.util.Scanner;

public class TestClass {
    public static void main(String args[]){
        int[] a1 = new int[10];
        int[] a2 = new int[10];
        int[] result = new int[10];

        System.out.print("\nEnter the contents for first array: ");
        Scanner sc=new Scanner(System.in);
        for(int i=0; i<10; i++){
            a1[i] = sc.nextInt();
        }
        System.out.println("\nEnter the contents for Second array: ");
        for(int i=0; i<10; i++){
            a2[i]=sc.nextInt();
        }

        AddSub addsub = new AddSub(a1, a2);

        System.out.print("\n******************************");
        System.out.print("\nArray One :\t");
        for(int i=0; i<10; i++){
            System.out.print(a1[i] + "  ");
        }
        System.out.print("\nArray Two :\t");
        for(int i=0; i<10; i++){
            System.out.print(a1[i] + "  ");
        }
        System.out.print("\n\n******************************");
        result = addsub.addition();
        result = addsub.sorting(result);
        System.out.print("\nSum of two arrays");
        System.out.print("\nSum :\t");
        for(int j=0;j<10;j++){
            System.out.print(result[j] + "  ");
        }
        System.out.print("\n\n******************************");
        result = addsub.subtraction();
        result = addsub.sorting(result);
        System.out.print("\nSubtraction of two arrays");
        System.out.print("\nSub :\t");
        for(int j=0;j<10;j++){
            System.out.print(result[j] + "  ");
        }
    }
}

Sub Class One

public class Array {
    int arr1[];
    int arr2[];
    int result[];

    Array(int arr1[], int arr2[]){
        this.arr1 = arr1;
        this.arr2 = arr2;
        result =  new int[10];
    }
}

Sub Class Two

public class AddSub extends Array{
    AddSub(int arr1[], int arr2[]){
        super(arr1, arr2);
    }

    public int[] addition(){
        for(int j=0; j<10; j++){
            result[j]=arr1[j]+arr2[j];
        }
        return result;
    }

    public int[] subtraction(){
        int z[] = new int[10];
        for(int j=0; j<10; j++){
            result[j]=arr1[j]-arr2[j];
        }
        return result;
    }

    static public int[] sorting(int[] x){
        for(int i=0; i<9; i++){
            for(int j=0; j<10-i-1; j++){
                if(x[j] > x[j+1]){
                    int temp = x[j+1];
                    x[j+1]= x[j];
                    x[j] = temp;
                }
            }
        }
        return x;
    }
}

You can Handle Exception using

   public  function callme()
   {
   try
   {
   for(int i=0; i<10; i++)
   {
    a1[i] = sc.nextInt();
   }
   System.out.println("\nEnter the contents for Second array: ");
   for(int i=0; i<10; i++)
   {
    a2[i]=sc.nextInt();
   }
}
catch(Exception e)
{
 System.out.println("Please Enter Correct Value");
 callme();
}
}

You have to use a try/catch block. The functionallity is pretty simple but you can make it as complex as you like to. Here is the basic example:

public static void main(String args[])
{
try
{
    // All of your current Code
}
catch (Exception ex)
{
    System.out.print("An error ocurred: " + ex.getMessage());
}
}

If you want to make it a little bit more specific, you can also put a try catch block in each function print the message and then throw the exception for the main code to catch it

public int[] subtraction()
{
try
{
    int z[] = new int[10];
    for(int j=0; j<10; j++)
    {
        result[j]=arr1[j]-arr2[j];
    }
    return result;
}
catch(Exception ex)
{
    System.out.print("Error in function substraction...");
    throw ex;
}
}

And so on, you can make a lot of things.this is just the basic for handling exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM