• Post Reply Bookmark Topic Watch Topic
  • New Topic

What Exceptions Stop the code in between?  RSS feed

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was actually looking for those exceptions which stops the Program in between not caring what is written in catch.

for eg:


Now i need a type of exception which just move only in catch and does not print "Hi".

My friend told that System exception stops the program whether you write anything in catch.

Please explain

thankyou
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You catch Exception, which means that all exceptions will be caught and "Hi" will be printed. You're not limited by exceptions though. There is no such thing as a system exception, but I think your friend meant errors - java.lang.Error and its sub classes. Error extends Throwable just like Exception, so you can throw it. Just like RuntimeException you don't need to catch it or declare to throw it.

Be warned though - throwing errors yourself is generally not a good idea. Programmers usually don't catch them so they will crash your application.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your aim is to avoid executing the code after the catch: it's not the type of Exception that matters, it's what you do with it. If you put return in your catch block, for instance, you'll exit the method and skip the following lines. Or you could rethrow the Exception (or even not catch it in the first place).

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