aspose file tools*
The moose likes Beginning Java and the fly likes throws the same as try catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws the same as try catch" Watch "throws the same as try catch" New topic
Author

throws the same as try catch

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hello,

is this:



the same as this:


Sergio Campos J.
Ranch Hand

Joined: Jun 01, 2009
Posts: 53
Hi!
Some question marks seem to be required somewhere here
for your post to be taken as a question.


"There are times when the easy things should be quick and easy" [...] "and worry about the theory later" -Fred Hamilton-
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

No They are different.



This method throws an exception and the function, which calls this method, has to either handle the exception or throw the exception. Otherwise compilation error occurs.




Exception is handled in this method instead of throwing the exception to caller of this method.

SCJP 5.0, SCWCD 5, preparing for SCDJWS
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Imagine, a failure occurs, then both versions prints the "e.printStackTrace();", am I right?

So this



implicitly calls the "e.printStackTrace()", when an error occurs!?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

No, the throws clause does NOT automatically print out the stacktrace! It just tells the compiler/virtual machine that the the thrown exception should be passed to the next method up the calling stack, i.e. the method that called this method!

What you probably mean instead is the fact the in a normal Java program the stacktrace is printed to the console if no method handles the exception until it reaches the main() method and main() doesn't handle it either. What you see then is the stacktrace output for the whole calling stack which is printed by the JVM.

Marco
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay, thanks !!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Marco Ehrentreich wrote:No, the throws clause does NOT automatically print out the stacktrace! It just tells the compiler/virtual machine that the the thrown exception should be passed to the next method up the calling stack, i.e. the method that called this method!

What you probably mean instead is the fact the in a normal Java program the stacktrace is printed to the console if no method handles the exception until it reaches the main() method and main() doesn't handle it either. What you see then is the stacktrace output for the whole calling stack which is printed by the JVM.

Marco


And just to be clear - if this happened (the exception gets propagated back to the main method) then entire application comes to a screeching halt. Whereas if you catch the exception then the exception is 'handled' and the rest of your application can continue to execute.


Steve
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

And just to be clear - if this happened (the exception gets propagated back to the main method) then entire application comes to a screeching halt. Whereas if you catch the exception then the exception is 'handled' and the rest of your application can continue to execute.


Of course I should have mentioned that.

And to add one more point: A key to good exception handling design of your application is to know where and how to handle exceptions! You should only catch exceptions in a method which is able to really handle it. To "handle" an exception you should do something more than just log and ignore the exception even though many examples make this seem to be a good solution. At least the user should be informed with a meaningful message that something has gone wrong and maybe what exactly went wrong when the exception occurred. After this you should reasonably clean up the state of the application to let the user continue his work, for example by correcting some input values.

Furthermore it's considered a bad idea to use the very generic exception type "Exception". This makes the compiler happy but it is a bad practice as the type (=class) of an exception alone can be valuable enough to see what has happened without any message just by looking at the type of the exception (e.g. CustomerNotFoundException). Additionally some exceptions may get "swallowed" in your example because "Exception" catches or throws any subclasses of it as well.

Marco


Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throws the same as try catch