• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling  RSS feed

 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to understand excption in java and confess the results were not as antecipated:


and


The questions are:

a) if I comment out the part 2 (only the line with the myMethod2 call, exactly as shown in the listing above) I get the the response below. Why do we see two messages? I thought that capturing one exception meant NOT to throw it.

D:\javastuff\temp>java ExceptionTest
myException has been thrown
myException has been captured

b) On the other hand, if I comment out the part1 (the try/catch block) I get the result below. Why, unlike the case a) above, is the stack trace shown?

D:\javastuff\temp>java ExceptionTest
myException has been thrown
Exception in thread "main" MyException
at ExceptionTest.exceptionThrower(ExceptionTest.java:5)
at ExceptionTest.myMethod1(ExceptionTest.java:11)
at ExceptionTest.myMethod2(ExceptionTest.java:17)
at ExceptionTest.main(ExceptionTest.java:28)

Thanks,

Cesar Olavo
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Cesar,
The explanation for part a is as follows:






As you can see above when the exceptionThrower() throws an exception a new instance of the MyException class is being created which causes the constructor for that class to be invoked.


Why do we see two messages?


And the constructor prints out the first message. Then when the exception is finally caught it prints out the second message.

I thought that capturing one exception meant NOT to throw it.


An exception is caught only after it is thrown.

Now in part b I will explain what I can because even I am not sure
what happens.

Most probably, the function of the main() method is to print the Stack trace if it is declared to throw exceptions.
 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shyam,

Thank you for you reply.


And the constructor prints out the first message. Then when the exception is finally caught it prints out the second message.
...
An exception is caught only after it is thrown.


You're right. I must confess my question was badly phrased though. What I really meant was:

Catching an exception means: "ok, the long-awaited event happened, but forget about the exception handler, I'm gonna handle this exception myself with the code in this catch clause."

Or not? Otherwise we would always have two codes executed -the catch and the handler (with that dreadful stack traces...)


Most probably, the function of the main() method is to print the Stack trace if it is declared to throw exceptions.


You mean the main method behaves differently from a regular one?


Cesar
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cesar Olavo:
Hi Shyam,

Thank you for you reply.



You mean the main method behaves differently from a regular one?


Cesar

Well, it's not that main() behaves differently really. Notice that you have declared main() to throw MyException. So where is it thrown to? The answer is that it is thrown back to the JVM that started your program. So the differences is in the the code that called main. The default exception handler in the JVM then prints out the stack trace. Esentially, this is the same behavior you see when your program accidentally throws a RuntimeException (e.g. NullPointerException, IndexOutOfBoundException, etc.) except that runtime exceptions don't have to be declared in the throws clause of a method.

Layne
 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shyam and Laine,

You both are absolutely right. Thank you for your explanations. Now the things seem clearer to me.

Cesar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!