• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception  RSS feed

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
1. Why do I get exception if I un-comment line 4 and comment line 3.

2. Look at the main method, still I could able to use "throw new SimpleException ();" , but I do not have reference for SimpleException
in the declaration of main method.

thanks
siva



 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In reply to your first question: when a method, either explicity -- as yours does -- or potentially, throws an exception, it must be declared as thrown. Otherwise you face a runtime exception if is thrown and the method is not expecting it.
[ August 01, 2004: Message edited by: Ryan Smith ]
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. BTW, look at the main method, I have declared thrown, still I have no problem. Correct ?
thanks
siva
 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the part of your code that I'm having trouble with:



The first statement in your try block will always execute; I'm not sure what you're trying to accomplish by putting that there. The second System.err.println statement will never execute because f will always throw an exception...Your catch block is always catching the exception throw by f(), not the SimpleException thrown at the end of the try block. Hope this clears things up.
[ August 01, 2004: Message edited by: Ryan Smith ]
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Smith,
My question is,
- look at the method f(), I have declared throws.
So, it makes sense for me to use stmt. "throw new SimpleException ();"
in that method.
If I don't have that declaration, I am getting compilation error.
Makes sense, correct ?.

- However, if you look at the method main(), I have not declared throws.
But, still I am able to use stmt "throw new SimpleException ()",
with no exception or issues. I wonder why ?. Please help me to understand.
[ August 01, 2004: Message edited by: Siva kandasamy ]
 
Weij Yin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, if you look at the method main(), I have not declared throws. But, still I am able to use stmt "throw new SimpleException ()",
with no exception or issues. I wonder why ?. Please help me to understand.


That is, because you use statement "throw new SimpleException()" in the block of try, and also the block of the exception catch catchs the exception, SimpleException. If the stmt is used outside the try block, you have to write "public static void main (String []args) throws SimpleException". In this situation, SimpleException will be handled by the Java VM.

Weij
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You.
-siva
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!