Q:

Java find output programs (Exception Handling) | set 3

belongs to collection: Java find output programs

0

Find the output of Java programs | Exception Handling | Set 3: Enhance the knowledge of Java Exception Handling concepts by solving and finding the output of some Java programs.

Question 1:

class UserException extends Exception
{ 
    public UserException():super("User Exception generated")
    {
         
    } 
}

public class ExpEx
{
     public static void main(String []args)
     {
        try
        {
            UserException U = new UserException();
            
            throw U;
        }
        catch (Exception e)
        {
            System.out.println("Message: "+e.getMessage());
        }
     }
}

Question 2:

class UserException extends Exception {
  public UserException(String msg) {
    super(msg);
  }
}

public class ExpEx {
  public static void main(String[] args) {
    try {
      UserException U = new UserException("User Exception generated");

      throw U;
    }
    catch(Exception e) {
      System.out.println("Message: " + e.getMessage());
    }
  }
}

Question 3:

class UserException extends Exception
{ 
    public UserException()
    {
        
    } 
}

public class ExpEx
{
     public static void main(String []args)
     {
        try
        {
            UserException U = new UserException();
            
            throw U;
        }
        catch (Exception e)
        {
            System.out.println("Message: "+e.getMessage());
        }
     }
}

Question 4:

class UserException extends Exception {
  public UserException() {
    super("UserException");
  }
}

public class ExpEx {
  public static void main(String[] args) {
    try {
      UserException U = new UserException();

      throw U;
    }
    catch(UserException U) {
      throw U;
    }
    catch(Exception e) {
      System.out.println("Message: " + e.getMessage());
    }
  }
}

Question 5:

class UserException extends Exception {
  public UserException() {
    super("UserException");
  }
}

public class ExpEx {
  public static void main(String[] args) {
    try {
      throw new UserException();
    }
    catch(UserException U) {
      System.out.println("Message: " + U.getMessage());
    }
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question  1:

Output:

/ExpEx.java:3: error: ';' expected
    public UserException():super("User Exception generated")
                          ^
1 error

Explanation:

The above program will generate syntax error because of constructor defined in UserException class, which is given below,

public UserException():super("User Exception generated");
{
}

The correct way is given below to call the superclass constructor,

public UserException()    
{
    super("User Exception generated");
} 

Answer Question 2:

Output:

Message: User Exception generated

Explanation:

In the above program, we created a class UserException, here we inherit Exception class in the UserException class. The UserException class contains a parameterized constructor that calls the constructor of the Exception class user super keyword to set the exception message.

Now look to the main() method, here we created the object U of UserException class and throw the object U using the throw keyword that will be caught by catch block and print "Message: User Exception generated" on the console screen.

Answer Question 3:

Output:

Message: null

Explanation:

In the above program, we created a class UserException, here we inherit Exception class in the UserException class. The UserException class contains a no-argument constructor with an empty body.

Now look to the main() method, here we created the object U of UserException class and throw the object U using the throw keyword that will be caught by catch block and print "Message: null" on the console screen.

Answer Question 4:

Output:

/ExpEx.java:15: error: unreported exception UserException; 
must be caught or declared to be thrown
      throw U;
      ^
1 error

Explanation:

The above program will generate a syntax error because here we have thrown an exception from the catch block,

catch(UserException U)
{
    throw U;
}
catch (Exception e)
{
    System.out.println("Message: "+e.getMessage());
}

Here, exception object U caught by UserException class in the catch block and then we throw exception object U which is not possible directly.

Answer Question 5:

Output:

Message: UserException

Explanation:

In the above program, we created a class UserException, here we inherit Exception class in the UserException class. The UserException class contains a no-argument constructor that will call the constructor method of Exception class using the super keyword.

Now look to the main() method, here we have thrown anonymous object of Exception class using the throw keyword that will be caught by catch block and print "Message: UserException" on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Enumeration) | set 1... >>
<< Java find output programs (Exception Handling) | s...