Reputation:
I'm trying to learn java and I have a little problem with understanding how exceptions work.
When do we need to use throw after a condition? As an example
public Ex(String name, String prenom, int age) throws exceptionex {
if (name.length() < 3 && prenom.length() < 3 && age < 1) throw new exceptionex();
this.age = age;
this.name = name;
this.prenom = prenom;
}
excepetionex
is an empty class which extends Exception
.
And what is the difference between the previous example and this one?
public Ex(String name, String prenom, int age) {
if (name.length() < 3 && prenom.length() < 3 && age < 1)
try {
throw new exceptionex();
} catch (exceptionex e) {
e.printStackTrace();
}
this.age = age;
this.name = name;
this.prenom = prenom;
}
And what does printstackTrace
exactly do?
Do we always need to create another class if we wanted to customise an exception (exeptionex
here ) or we can just use (throw new Exception
)?
I googled my questions but i didn't find an answer which i can understand maybe because I'm new or because English is not my native language I need a simple explanation as much as possible please.
Upvotes: 2
Views: 98
Reputation: 31
The first snippet will interrupt the flow of the program before assigning the age, name and prenom, returning the control to the caller method, which will need to catch the exception.
The second snippet wont interrupt the flow of the program. It will print the stacktrace (which will be only your exception) and then assgin age, name and prenom.
printStackTrace just outputs the stack of calls on standard output when an exception happens, so you can see the full hierarchy of method calls to triage the cause of the exception.
The common practice when throwing exceptions is to have subclasses of Exception or Throwable which provide semantical information about what kind of error is happening. You have probably already seen NullPointerException, for instance.
In the example you are providing, I guess you could rename your exception to something like IvalidPersonalDetailsException, so when it is thrown, the caller code can know what went wrong.
Upvotes: 0
Reputation: 140328
what is the diffrence between the past example and this one !
The first one stops you creating an instance of Ex
if that condition is met.
The second one prints something to stderr, and then creates the instance anyway.
Note that you don't need to throw an exception to print its stack trace:
new exexception().printStackTrace();
does the same as the try/catch in the second example.
And what does printstackTrace exactly does
Your first stop should be Javadoc:
Prints this throwable and its backtrace to the standard error stream. This method prints a stack trace for this
Throwable
object on the error output stream that is the value of the fieldSystem.err
. The first line of output contains the result of thetoString()
method for this object. Remaining lines represent data previously recorded by the methodfillInStackTrace()
...
It goes on. Read it, and ask a specific question about what you don't understand.
Do we always need to create another class if we wanted to customise an exception (exeptionex here ) or we can just use (throw new Exception)!
You don't always need a new exception type. Effective Java 2nd Ed Item 60 advises to "Favor the use of standard exceptions".
Note that you basically never want to throw Exception
itself (and you rarely should catch it).
Upvotes: 1