if a program throw 2 exceptions can we handled it by user defined exception.for example

import java.util.*;
class WrongException extends Exception{

    public WrongException(String s){

        super(s);
    }
}
public class UserDefinedException2{

    public static void main(String s[]){

        int a,b;
        a=b=0;
        Scanner ob=new Scanner(System.in);
        System.out.print("Enter your age = ");
        a=ob.nextInt();
        try
        {
            if(a<=0)
            throw new WrongException("age cannot be zero or nagetive");
            System.out.print("age = "+a);
        }
        catch(WrongException e){

            System.out.print(e);
        }
    }
}

the upper code handled an negetive and zero value exception.there exception can occure that throw InputMismatchException then how we can handled it. now we will handled two exceptions my question is how two exception handle by user defined exception

What exectly do you mean by " how two exception handle"?
As soon as your code throws an exception, the rest of the block stops executing, so you cannot throw two exceptions at the same time.

ps. Your example code is perfectly legal, but also misleading. It's very odd to throw and catch an exception in the same method. Normally a method throws an exception because it found an error that it cannot handle. It throws the exception back up to the calling method so that can handle it. eg:

public static void main(String s[]){
  Scanner ob=new Scanner(System.in);
  System.out.print("Enter your age = ");
  int a=ob.nextInt();
  try {
     setAge(a);
  } catch(WrongException e) {
     System.out.print("You input was rejected because " +e.getMessage() + "  (details follow...)");
     e.printStackTrace();
  }
}

public static void setAge(int age) throws WrongException {
  if(age<=0)
     throw new WrongException("age cannot be zero or nagetive");
  System.out.print("age = "+age);
}

Edited 2 Years Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.