Java find output programs (Exception Handling) | set 3

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.
Submitted by Nidhi, on February 04, 2021

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());
        }
     }
}

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");
} 

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());
    }
  }
}

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.

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());
        }
     }
}

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.

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());
    }
  }
}

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.

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());
    }
  }
}

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.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.