Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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

gp.ShortPassException: The password provided is too short
      at gp.CatchMultipleExceptions.checkPass(CatchMultipleExceptions.java:106)
      at gp.CatchMultipleExceptions.main(CatchMultipleExceptions.java:15)
Finally block is always executed
gp.NoPassException: No pass provided
      at gp.CatchMultipleExceptions.checkPass(CatchMultipleExceptions.java:100)
      at gp.CatchMultipleExceptions.main(CatchMultipleExceptions.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.checkPass(CustomExceptionExample.java:57)
      at gp.CustomExceptionExample.main(CustomExceptionExample.java:30)
Caused by: java.lang.NullPointerException
      at gp.CustomExceptionExample.checkPass(CustomExceptionExample.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.ArithmeticException: / by zero
      at gp.CheckedUncheckedExceptions.divide(CheckedUncheckedExceptions.java:63)
      at gp.CheckedUncheckedExceptions.main(CheckedUncheckedExceptions.java:28)


I have not understood the output and flow and meaning of 'CheckedUncheckedExceptions'


Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> I have not understood difference between first link and second link example?
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

try {
} catch (NoPassException x) {
} catch (ShortPassException x) {
} finally {
}

Open in new window


you can just write

try {
} catch (InvalidPassException x) {
} finally {
}

Open in new window

Avatar of gudii9

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
e.printStackTrace(); 

Open in new window

the order of the tracing is not guaranteed.
When using
System.out.println("Exception: " + e);

Open in new window

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.
Avatar of gudii9

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


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

Open in new window


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:
public static void checkPass(String pass) throws NoPassException, ShortPassException {
       int minPassLength = 5;

       if (pass == null)
           throw new NoPassException("No pass provided");
...

Open in new window


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.
Avatar of gudii9

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??
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial