• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Exception handling  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody, i have a question.

This is the code from book. And I do not understand why in this case it shows 1 error even though there are two.
a: 0
divide by zero: java.lang.ArithmeticException: / by zero
After try/catch block


Although if we change a, we will get another result that ignores the first.
a: 1
Array index oob: java.lang.ArrayIndexOutOfBoundsException: 43
After try/catch block

What happens to error number 1 or 2?

Thank you in advance.
 
Saloon Keeper
Posts: 5757
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two pieces of code provided look the same. In either case you will not get a divide by zero exception, only an out of bounds exception.

If you did have two errors in the code, only the first one would trigger an exception because the execution would jump to the catch block and no longer be executing statements in order in the try block.
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us which book and page number, to reduce copyright problems.
If you throw an exception in line 8, you don't execute line 10, so you never see the second error.

By the way: How did you get an arithmetic exception? If a is args.length + 1, it can never be 0.
 
All of the following truths are shameless lies. But what about this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!