Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception. Chapter-6,Exercise 6-4 page-353(Sierra K,Bates OCA/OCP 7)

 
Akash Trivedi
Greenhorn
Posts: 18
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,well I want to know that is there any way to catch the IllegalArgumentException(if we don't give the argument, ofcourse) since it is uncatchable due to the ArrayIndexOutOfBoundsException!
Thanks


 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should check if sd is null before checking its length.
 
Akash Trivedi
Greenhorn
Posts: 18
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You should check if sd is null before checking its length.


It doesn't help,still got ArrayIndexOutOfBoundsException :-(
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line nine? You need to check the size of args first.

If it's not line nine, please post the full error message.
 
Akash Trivedi
Greenhorn
Posts: 18
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:On line nine? You need to check the size of args first.

If it's not line nine, please post the full error message.


However,i did this but no help.





ERROR

C:\Users\Mr_Nobody\java>java MyException
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MyException.main(MyException.java:9)
 
Liutauras Vilda
Bartender
Pie
Posts: 2802
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It seems that you're trying to solve your exercise in trial and error way. Usually it is not a good approach.

1. You wrote a lot of code which is badly indented - that complicates finding an error.
2. You wrote too much code without having confidence in its correctness, even about its smallest piece.

Please tell us what is the purpose of this exercise. Is it something related with OCA exam? Maybe tutor's assignment? Personal project?
Explain us what do you want to do in general - it might turns out it is not the way to solve it at all.
 
Campbell Ritchie
Sheriff
Posts: 51450
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have also misunderstood what null means. You can never get a null array as the command line arguments; the operating system always passes something on, even if it is an empty array. It does not contain nulls; all its elements will be real Strings. It is possible to pass a short String like this:-
java Foo ""
but that is a zero‑length String not null. You will simply confuse yourself if you go calling it null.
 
Akash Trivedi
Greenhorn
Posts: 18
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it is related to OCA. It was a simple question about user-defined exception,well as I approached I thought there must be some exception catching mechanism which throws an exception(user-defined) if no arg is passed .
Well,now I think it is already there(i.e ArrayIndexOutOfBoundsException) and I shouldn't override it. I just want to know if there is some way,we can avoid it by passing our own exception. I will appreciate if you enlighten me on how should I improve my programming methodology.
Thanks :-).
 
Liutauras Vilda
Bartender
Pie
Posts: 2802
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akash Trivedi wrote:Yeah it is related to OCA.
I have referenced your topic in our dedicated OCA certification forum.

I have to admit I do not understand the actual problem you're trying to solve. I do understand the mechanics what is wrong and why you're getting such error, but probably you understand that yourself.
You can try to write down the chapter (numbers) you're refering to, so someone could understand the actual topic's context and which subject it is meant to teach.
 
Akash Trivedi
Greenhorn
Posts: 18
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Akash Trivedi wrote:Yeah it is related to OCA.
I have referenced your topic in our dedicated OCA certification forum.

I have to admit I do not understand the actual problem you're trying to solve. I do understand the mechanics what is wrong and why you're getting such error, but probably you understand that yourself.
You can try to write down the chapter (numbers) you're refering to, so someone could understand the actual topic's context and which subject it is meant to teach.


I edited the topic's name.Is it okay.?
Thanks.
 
Liutauras Vilda
Bartender
Pie
Posts: 2802
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akash Trivedi wrote:I edited the topic's name.Is it okay.?
Yes, that way is much better I think. I don't have myself that book (I have two others), but that book is popular among candidates, so be patient, and you should get more feedback.
 
Junilu Lacar
Bartender
Pie
Posts: 8886
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to what Liutauras said about guessing and starting with only some code, then building up the program slowly as you see things working (called "incremental development"), here are a few things to think about:

1. An expression like arg.length where arg is an array will throw a NullPointerException if arg is null. Figure out how to check that condition in Java

2. An expression like arg[0] tries to reference the first element of the array arg. If arg has no elements, that is, if there is not even a first element in array arg, then the expression arg[0] will cause an ArrayIndexOutOfBoundsException to be thrown.

Now, THINK.

In what order should you check these conditions? The order of checking is important because failure to check one of these conditions first will preclude checking the other condition.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Akash Trivedi wrote:I edited the topic's name.Is it okay.?
Yes, that way is much better I think. I don't have myself that book (I have two others), but that book is popular among candidates, so be patient, and you should get more feedback.

Besides a bunch of practice questions and mock exams, K&B7 has also some coding exercises. And this is one of them. The purpose of this coding exercise is actually very simple: you have to create a checked exception (BadFoodException) and then write a program to determine whether a command-line argument representing a particular food (as a string) is considered bad or okay. That's it!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akash Trivedi,

First of all, a warm welcome to CodeRanch!

Akash Trivedi wrote:well as I approached I thought there must be some exception catching mechanism which throws an exception(user-defined) if no arg is passed .
Well,now I think it is already there(i.e ArrayIndexOutOfBoundsException) and I shouldn't override it. I just want to know if there is some way,we can avoid it by passing our own exception. I will appreciate if you enlighten me on how should I improve my programming methodology.

Let's have a look at the second code snippet you have posted and give some advice about your programming methodology. A first (very) important general remark: the indentation of your code is very poor and that makes it really hard to read. And a second (very) important general remark: always use meaningful names for variables, methods, classes,... It will improve the readibility of your code as well. So try to avoid using one-letter variables like d, i,... (unless for loop counters in a for loop) or meaningless abbreviations like arr.

Let us first have a look at the BadFoodException class. This class meets the requirements of the coding exercise Just a minor remark: you could provide the bad food as a string to the exception and thus provide a little bit more information. Here's the adjusted BadFoodException classNow it is time to focus on the MyException class. The signature of the main() method is according to the instructions But the implementation of this method is a bit of a mess (and has a few mistakes). The purpose of this class is to determine whether a command-line argument representing a particular food (as a string) is considered bad or okay. So when you want to run this class, you must provide one command-line argument when invoking the java command (e.g. java MyException hamburger). When an application is launched, the runtime system passes the command-line arguments to the application's main() method via an array of Strings (as you probably know). The array of the main() method will never be null; if no command-line arguments are provided to the application, this array will be empty (contain 0 elements).
So you first have to check if the array of the main() method contains exactly 1 (one) element. If it doesn't, you print an error message to the user and exit this method. If the array has exactly 1 element, you can invoke the checkFood() method and verify if the food is bad or okay. This check is missing in your application and therefore you are getting a java.lang.ArrayIndexOutOfBoundsException if you try to run the application using a command-line invocation without command-line arguments (e.g. java MyException).
Here is my version of the MyException classAnd here is an overview of some invocations of the above application (with the corresponding output)Please notice there are a few differences with your MyException class:
  • The getMessage() method of class Exception returns already a String, so there is no reason to concatenate the value with an empty String
  • in this scenario there is no added value to print the stack trace of the BadFoodException class
  • the checkFood() method should only throw a BadFoodException, but should not print an "I like it very much"-message. You maybe want to reuse the checkFood() method in another method where you don't want such a "I like it very much"-message to be printed. Printing this message is the responsibility of the main() method.


  • Hope it helps!
    Kind regards,
    Roel

    PS. See how much easier it is to read properly indented code using meaningful names for variables, methods, classes, and so on
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic