gudii9
asked on
multiple exceptions
Hi,
I was working on below example
http://examples.javacodegeeks.com/java-basics/exceptions/catch-and-throw-multiple-exceptions-example/
I got output as below
i have not understood output, exception flow .
when i ran other example from below link on custom exception
http://examples.javacodegeeks.com/java-basics/exceptions/create-custom-exception-example/
I got below similar output
gp.InvalidPassException: No password provided
at gp.CustomExceptionExample. checkPass( CustomExce ptionExamp le.java:57 )
at gp.CustomExceptionExample. main(Custo mException Example.ja va:30)
Caused by: java.lang.NullPointerExcep tion
at gp.CustomExceptionExample. checkPass( CustomExce ptionExamp le.java:49 )
... 1 more
I have not understood difference between first link and second link example?? Both seems same to me.
I also saw one other example as below
http://examples.javacodegeeks.com/java-basics/exceptions/checked-and-unchecked-exceptions-example/
When i ran output as below
Exception in thread "main" java.lang.ArithmeticExcept ion: / by zero
at gp.CheckedUncheckedExcepti ons.divide (CheckedUn checkedExc eptions.ja va:63)
at gp.CheckedUncheckedExcepti ons.main(C heckedUnch eckedExcep tions.java :28)
I have not understood the output and flow and meaning of 'CheckedUncheckedException s'
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
I was working on below example
http://examples.javacodegeeks.com/java-basics/exceptions/catch-and-throw-multiple-exceptions-example/
I got output as below
gp.ShortPassException: The password provided is too short
at gp.CatchMultipleExceptions.checkPass (CatchMult ipleExcept ions.java: 106)
at gp.CatchMultipleExceptions.main(Catc hMultipleE xceptions. java:15)
Finally block is always executed
gp.NoPassException: No pass provided
at gp.CatchMultipleExceptions.checkPass (CatchMult ipleExcept ions.java: 100)
at gp.CatchMultipleExceptions.main(Catc hMultipleE xceptions. java:41)
i have not understood output, exception flow .
when i ran other example from below link on custom exception
http://examples.javacodegeeks.com/java-basics/exceptions/create-custom-exception-example/
I got below similar output
gp.InvalidPassException: No password provided
at gp.CustomExceptionExample.
at gp.CustomExceptionExample.
Caused by: java.lang.NullPointerExcep
at gp.CustomExceptionExample.
... 1 more
I have not understood difference between first link and second link example?? Both seems same to me.
I also saw one other example as below
http://examples.javacodegeeks.com/java-basics/exceptions/checked-and-unchecked-exceptions-example/
When i ran output as below
Exception in thread "main" java.lang.ArithmeticExcept
at gp.CheckedUncheckedExcepti
at gp.CheckedUncheckedExcepti
I have not understood the output and flow and meaning of 'CheckedUncheckedException
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Concerning your first example, I advise you to replace
e.printStackTrace();
1:
Select allOpen in new window
by
System.out.println("Exception: " + e);
what is the difference between above two. Please advise
When running the program you linked to, I experienced that when using
When using
Of course e.printStackTrace() gives you more information, but for illustrating of the use of exceptions, the difference between both doesn't really matter.
e.printStackTrace();
the order of the tracing is not guaranteed. When using
System.out.println("Exception: " + e);
it is. That sequential order makes the output a lot easier to understand.Of course e.printStackTrace() gives you more information, but for illustrating of the use of exceptions, the difference between both doesn't really matter.
ASKER
When running the program you linked to, I experienced that when using
e.printStackTrace();
1:
Select allOpen in new window
the order of the tracing is not guaranteed.
When using
System.out.println("Exception: " + e);
1:
Select allOpen in new window
it is. That sequential order makes the output a lot easier to understand.
How it is making sequesntion with--> system out where printing randomly with -->e.printStackTrace();
First, a too short password is provided, then no password is provided and then a correct one is provided.
How passing null considered as passing no passwor as below
// We demonstrate with no password
try {
CatchMultipleExceptions.checkPass(nu ll);
why we need separate classes for short and no password etc as below
//A custom business exception for no password
class NoPassException extends Exception {
NoPassException() {
}
NoPassException(String message) {
super(message);
}
NoPassException(String message, Throwable cause) {
super(message, cause);
}
}
// A custom business exception for short password
class ShortPassException extends Exception {
ShortPassException() {
}
ShortPassException(String message) {
super(message);
}
ShortPassException(String message, Throwable cause) {
super(message, cause);
}
}
please advise
>> How it is making sequesntion with--> system out
>> where printing randomly with -->e.printStackTrace();
I don't know. I just experienced it myself and that's why I made those changes.
>> How passing null considered as passing no passwor as below
In java each variable that can contain an Object (or something that extends Object - which is every class) initially has the value null, meaning "nothing".
That corresponds with this code:
The method checks if the passed in function parameter pass (being a String) is null. If it is an exception is thrown indicating that no password was supplied.
>> why we need separate classes for short and no password etc as below
You don't need to do it that way. That's the code writer's choice.
That's exactly the difference between the 1st and the 2nd example, as I told you earlier.
>> where printing randomly with -->e.printStackTrace();
I don't know. I just experienced it myself and that's why I made those changes.
>> How passing null considered as passing no passwor as below
In java each variable that can contain an Object (or something that extends Object - which is every class) initially has the value null, meaning "nothing".
That corresponds with this code:
public static void checkPass(String pass) throws NoPassException, ShortPassException {
int minPassLength = 5;
if (pass == null)
throw new NoPassException("No pass provided");
...
The method checks if the passed in function parameter pass (being a String) is null. If it is an exception is thrown indicating that no password was supplied.
>> why we need separate classes for short and no password etc as below
You don't need to do it that way. That's the code writer's choice.
That's exactly the difference between the 1st and the 2nd example, as I told you earlier.
ASKER
>>You don't need to do it that way. That's the code writer's choice.
can i still get individual customized exception for each case like short password, no password etc??
can i still get individual customized exception for each case like short password, no password etc??
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the first example they use two different Exceptions for the two different cases: NoPassException and ShortPassException.
In the second only one is used: InvalidPassException (and of course in that case you only have to catch the one). So, instead of writing
Open in new window
you can just write
Open in new window