user3659934
user3659934

Reputation: 15

Default constructor of class Exception?

package com.psl.exception;

public class NoDataFoundException extends Exception {

    public NoDataFoundException(){
        super("No Data Found");
    }

    public NoDataFoundException(String msg){
        super(msg);
    }
}

What does the class Exception do with the string that is passed? I tried it and it does nothing. What is the point of passing the string to the constructor of Exception then?

Upvotes: 0

Views: 2097

Answers (3)

Adam Yost
Adam Yost

Reputation: 3625

One of the constructors of Exception takes a string to use as the detail message.

See here: http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

The default constructor uses a null detail message, but other components such as a message and throwable can be specified

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074266

The documentation says:

Constructs a new exception with the specified detail message.

The detail message is available from getMessage and usually included in toString and such.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

It sets String passed as message in Throwable, which is generally a useful custom message which gets printed when exception is thrown, also can be retrieved by getMessage() method on Throwable

Upvotes: 1

Related Questions