• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws the same as try catch

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

is this:



the same as this:


 
Sergio Campos J.
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Some question marks seem to be required somewhere here
for your post to be taken as a question.
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, thanks !!
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic