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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
Concerning your first example, I advise you to replace
e.printStackTrace();

Open in new window

by
System.out.println("Exception: " + e);

Open in new window


and to number the three statements:
System.out.println("Finally block is always executed");

Open in new window

like
System.out.println("[1] Finally block is always executed");
...
System.out.println("[2] Finally block is always executed");
...
System.out.println("[3] Finally block is always executed");

Open in new window

and to run the program again.

You'll get this ouput:

Exception: playfield.exceptions.ShortPassException: The password provided is too short
[1] Finally block is always executed
Exception: playfield.exceptions.NoPassException: No pass provided
[2] Finally block is always executed
Password check : OK
[3] Finally block is always executed

Open in new window

which is rather clear, I guess.
First, a too short password is provided, then no password is provided and then a correct one is provided. And the finally block is always reached.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
>> 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

0
gudii9Author Commented:
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
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

zzynxSoftware engineerCommented:
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.
0
gudii9Author Commented:
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
0
zzynxSoftware engineerCommented:
>> 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.
0
gudii9Author Commented:
>>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??
0
zzynxSoftware engineerCommented:
>> can i still get individual customized exception for each case like short password, no password
Yes. As I said: that's your choice.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.