This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Catch block should not have been triggered?

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple try-catch-finally block program:

The output is this:

a
Now in try
b
Now in catch
c
d
e

What I don't understand is why the catch block was triggered (evidently since lines 13 and 14 printed their strings).
How does the simple print statement on line 10 trigger an IllegalArgumentException?
 
Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:...How does the simple print statement on line 10 trigger an IllegalArgumentException?


Hi Sam,
It is not the print statement on line 10 that is throwing an IllegalArgumentException, but line 11 is. The IAE is a subclass of RuntimeException and is therefore caught by the catch block. The code in the catch block executes, followed by the finally block and the remaining code.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:

Sam Peterson wrote:...How does the simple print statement on line 10 trigger an IllegalArgumentException?


Hi Sam,
It is not the print statement on line 10 that is throwing an IllegalArgumentException, but line 11 is. The IAE is a subclass of RuntimeException and is therefore caught by the catch block.



I already knew that the finally block is supposed to automatically execute, but why is line 11 throwing the exception of there was no compilation error? What good is throwing exceptions if they are still going to be thrown when they don't need to be thrown?
 
Brecht Geeraerts
Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 11 is throwing an exception because you are literally telling it to: "throw new IllegalArgumentException();"

A compilation error is very different from an Exception. Have a look at this StackOverflow post on the difference between compilation errors and runtime exceptions.

In the code snippet you posted there is absolutely no need (programatically) to throw an IllegalArgumentException there. I presume it was written this way to test ones understanding of try-catch-finally blocks.

 
Rancher
Posts: 3656
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also posted here:  https://www.java-forums.org/new-java/97531-catch-block-should-not-have-been-triggered.html
Please read: https://coderanch.com/wiki/660346/Wiki/Forthright-Cross-Posting-Sites

Look at the API doc for the IllegalArgumentException class and you will see that it extends the RuntimeException class.
Therefore the catch block is triggered.
 
Sheriff
Posts: 15048
252
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:I already knew that the finally block is supposed to automatically execute, but why is line 11 throwing the exception of there was no compilation error? What good is throwing exceptions if they are still going to be thrown when they don't need to be thrown?


It seems you have a fundamental misunderstanding of the workings of exceptions and the try-catch-finally.

First, this: "why is line 11 throwing the exception of[sic] there was no compilation error?"  

Exceptions are not thrown during compilation. Exceptions are always thrown during runtime. You get compilation errors if your code contains syntax errors and violations of rules that the compiler checks, like type compatibility, definite assignment, and such. All those rules are laid out by the Java Language Specification (JLS). There's a big difference between throwing an Exception or any of its subclasses and getting a compilation error. Know the distinction.

Second, this: "What good is throwing exceptions if they are still going to be thrown when they don't need to be thrown?"

This is non-sensical and is a strong indication of some basic misunderstanding on your part. That's like saying "Why did you jump when I told you to jump?"

The IllegalArgumentException is thrown precisely because that code says it should be thrown:

What else do you think that statement tells the computer to do?
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!