• Post Reply Bookmark Topic Watch Topic
  • New Topic

exceptions  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , i have read exceptions but still i havent managed to understand what really and how to use them so i will make an explanation on my exercise if i understand it good .
i should give a number from keyboard(userinput) :String 9 length.But 5 of it will be latinik albafet and to have 4 numbers (0 -9).if the userinput is in right i will system.out this number if not i should make an exception * invalidNumberException.



* here what should i use ? method ? to create a class ? to make try { } catch{} .My mind here stuck
 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will recommend to do validation with Javascript.

And from Java validate and throw a exception when the user submit something that you dont want to receive or any other kind of error could happen.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, create a method, and a class to encapsulate that exception. Start by reading about Exceptions in the Java™ Tutorials.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mauro Trevigno wrote:I will recommend to do validation with Javascript. . . .
Why? There is nothing to suggest that OP is using any JS.
 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mauro Trevigno wrote:I will recommend to do validation with Javascript. . . .
Why? There is nothing to suggest that OP is using any JS.


You're right, could be from console.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mauro Trevigno wrote:I will recommend to do validation with Javascript. . . .
Why? There is nothing to suggest that OP is using any JS.

Moreover, in a web application, client-side validation should never be trusted. It's nice for the user, but the server must validate all input even if it's already been validated on the client.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot . I wil send what I did.I have and problem on this and as I see I have forgot the limit A-Z."String 9 length.But 5 of it will be latinik albafet (A-K)and to have 4 numbers (0 -9)."
So what i did but it doesn't work
x =UserString//lets say user give A-K .I don't know how can I do this limit and I should make this give only a char.He give lets say A.Only caps locks
k=UserString // Lets say F
c=UserString//lets say E
l=UserString// lets say D
So Code=+ x + k + c + l;// so
AFED that I want to create but it doesn't worked
After with the same tactic for integer to create 4 numbers and every one of them will be 0-9 so that will be together example with the same way give the user 5693 .
I want to add them now together AFED5693 .How could I do this too? Thanks a lot
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte,
Questions that amount to here is my assignment, please help me, rarely ever get the help you want, but specific questions usually get a quick and concise response. So let me break this down:
i should give a number from keyboard(userinput)

can you do that part? input from the keyboard? (quite often that is done using the Scanner class)
i should give a number from keyboard(userinput)

can you verify a Strings length? You can look up string methods in the String API
But 5 of it will be latinik albafet and to have 4 numbers (0 -9)

This is the, in my opinion, the more difficult part of the assignment. You will need to make a list of characters that you want to accept as input--make a String--so if I wanted to accept a-f and 0-4 , I would make the String "abcdef01234" and then check to see if each character in your input string is contained in your target String. This information is also contained in the String API.

So if any of your validations didn't pass, then you need to throw your Exception. You can look up Exception in the API also and see how to make a new Exception with your specific messages you want to have.

All of this needs to be wrappered in a new object of your design--a normal Java program.

So which part of this do you need help with?
ekte spiriopoulos wrote:Hello , i have read exceptions but still i havent managed to understand what really and how to use them so i will make an explanation on my exercise if i understand it good .
i should give a number from keyboard(userinput) :String 9 length.But 5 of it will be latinik albafet and to have 4 numbers (0 -9).if the userinput is in right i will system.out this number if not i should make an exception * invalidNumberException.



* here what should i use ? method ? to create a class ? to make try { } catch{} .My mind here stuck
 
bhanuprasad reddy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your question I think that you know how to validate user input, as you need to know about implementing about *

Create a class with name InvalidNumberException and make exception as its super class



Once validation fails throw new InvalidNumberException("number not valid") with a message like this.

To understand more on this topic you can read about UserException.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:ekte,
Questions that amount to here is my assignment, please help me, rarely ever get the help you want, but specific questions usually get a quick and concise response. So let me break this down:
i should give a number from keyboard(userinput)

can you do that part? input from the keyboard? (quite often that is done using the Scanner class)
i should give a number from keyboard(userinput)

can you verify a Strings length? You can look up string methods in the String API
But 5 of it will be latinik albafet and to have 4 numbers (0 -9)

This is the, in my opinion, the more difficult part of the assignment. You will need to make a list of characters that you want to accept as input--make a String--so if I wanted to accept a-f and 0-4 , I would make the String "abcdef01234" and then check to see if each character in your input string is contained in your target String. This information is also contained in the String API.

So if any of your validations didn't pass, then you need to throw your Exception. You can look up Exception in the API also and see how to make a new Exception with your specific messages you want to have.

All of this needs to be wrappered in a new object of your design--a normal Java program.

So which part of this do you need help with?
ekte spiriopoulos wrote:Hello , i have read exceptions but still i havent managed to understand what really and how to use them so i will make an explanation on my exercise if i understand it good .
i should give a number from keyboard(userinput) :String 9 length.But 5 of it will be latinik albafet and to have 4 numbers (0 -9).if the userinput is in right i will system.out this number if not i should make an exception * invalidNumberException.



* here what should i use ? method ? to create a class ? to make try { } catch{} .My mind here stuck
1) i can give a number(UserInput i use ) (scanner i dont know yet how to use it because i havent try with it)
2) i cant verify a String length my friend.I will now look this .Thanks a lot.
3)Campbell Ritchie help me with exception.You and the bhanuprasad reddy help me with the the content
i will send what i did to check it Thanks for help
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
i have lost it a little .I think to create an array of 5 int .And when array[0] will contain all characters from A-K. additional array[1] the same,array[2] //until array[5].Can it happen that somehow? i want userinput to give on the array[0] some of A-K .Is good my think?

exampe

is this good?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:. . . I think to create an array of 5 int .And when array[0] will contain all characters from A-K. additional array[1] the same,array[2] //until array[5].Can it happen that somehow? i want userinput to give on the array[0] some of A-K .Is good my think? . . .
I am not sure I understand your question. You can of course check whether a char is in a certain range because a char is not a letter but a number. Do you want a 5‑element array containing values between 'A' and 'K' inclusive? Why do you want an array? What are you trying to do?
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
ekte spiriopoulos wrote:. . . I think to create an array of 5 int .And when array[0] will contain all characters from A-K. additional array[1] the same,array[2] //until array[5].Can it happen that somehow? i want userinput to give on the array[0] some of A-K .Is good my think? . . .
I am not sure I understand your question. You can of course check whether a char is in a certain range because a char is not a letter but a number. Do you want a 5‑element array containing values between 'A' and 'K' inclusive? Why do you want an array? What are you trying to do?
Thats exactly i want to do to have the option for array[0] to have option between A-K and the others : array[1] to have A-K until array[5] its confusing.I need to use and userinput but i dont know where i could do that to have the choice between A-K
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need that array of letters? It should be easy enough to fill by taking a 5‑letter input from the keyboard.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why do you need that array of letters? It should be easy enough to fill by taking a 5‑letter input from the keyboard.
could you make an example what you mean? because i cant understand ( i have thought that it will be easy with array).Or do a similar example or send me an exercise if you could to understand what should i do in this case
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte,
String has well defined methods to manipulate the String directly, so no need to break your letters apart. Here is an example of one way:

ekte spiriopoulos wrote:
Campbell Ritchie wrote:Why do you need that array of letters? It should be easy enough to fill by taking a 5‑letter input from the keyboard.
could you make an example what you mean? because i cant understand ( i have thought that it will be easy with array).Or do a similar example or send me an exercise if you could to understand what should i do in this case
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
i did this with exception but it doesnt work the exception when i press more than 60 .What I have wrong
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone can help with this?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you never really throw an exception. You declare that an exception will be thrown in the method, but you never throw an exception. You need to make a false clause on your "if" statement and throw the exception there.

ekte spiriopoulos wrote:
Campbell Ritchie wrote:ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
i did this with exception but it doesnt work the exception when i press more than 60 .What I have wrong
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:That's because you never really throw an exception. You declare that an exception will be thrown in the method, but you never throw an exception. You need to make a false clause on your "if" statement and throw the exception there.

ekte spiriopoulos wrote:
Campbell Ritchie wrote:ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
i did this with exception but it doesnt work the exception when i press more than 60 .What I have wrong
it worked now but its right now yes? i did it with exceptions not with IOexception
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is definitely NOT correct.

ekte spiriopoulos wrote:
Les Morgan wrote:That's because you never really throw an exception. You declare that an exception will be thrown in the method, but you never throw an exception. You need to make a false clause on your "if" statement and throw the exception there.

ekte spiriopoulos wrote:
Campbell Ritchie wrote:ES: Why do you need an exception by that name? Have you looked through the different types of exception available to see whether there is something already available?
BR: Why did you suggest java.lang.Exception as the direct superclass of the new exception type? What is the drawback of that approach, which micht even make it an incorrect approach?
ES: Why can you make an int[] containing 'A', 'B' etc?
i did this with exception but it doesnt work the exception when i press more than 60 .What I have wrong
it worked now but its right now yes? i did it with exceptions not with IOexception
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte,
you have 2 things wrong with your code:

1 - you never throw an exception. (here is a tutorial on exceptions read it, study it, understand it).

You are so lost on this, that you don't even understand the answers people are giving you. YOU have to stop, take some time, and do some learning. If you don't, then all you are
doing is begging code. We are here to increase your understanding, not to do your homework for you. That requires that you continue learning and not just throw code at the
problem while you are freaking out about not having the assignment done. Take a deep breath, relax, go out to the tutorial and read and study it until it makes sense. Your code
is not any more correct now than it was when you started this thread. Learn what you need to learn in the tutorial, that is the only way you are getting this done, because we are
not going to give you code.

2 - the Exception you are not using, as asked for in your assignment, is no where in your code.

If your instructor asked for you to use a certain Exception, then putting in something else is not going to get you points in the assignment. So, you have to decide if your code is
compliant with the instructors wishes and requests. Know this: your code is not working according to the assignment in any condition.

If you choose to continue with programming you will have to learn that running code is not necessarily code that is correct. It has to do what it is supposed to do, and even more
it must fit the need it was created for. Yours does none of that except that it compiles and puts out some IO. That IO is not appropriate, nor is the process appropriate for the task,
the assignment, you were given. So once again, I have to say: breath, relax, go study the tutorial and learn.
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:ekte,
you have 2 things wrong with your code: . . .
he just say to do exceptions he doesnt care which one Anyway with this exception this worked .Thanks a lot and your advice
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't quote the whole of the preceding post; that simply makes the thread longer. I have removed most of the quote.

We have an FAQ entitled, “It Doesn't Work Is Useless”. Maybe we should have one called “It Works Is Useless”, too . As LM has said, simply getting code to run does not mean there is anything good about it.
I am surprised if your instructor has given you such vague advice. You appear to have been told to create an InvalidNumberException. You were given some (I think not good) advice about how to create that. I suggested you do some looking around to find other Exceptions. Did you look through the tutorial which somebody gave you a link to? Now you appear to be using IOExceptions instead. Why? How did you get your teacher to tell you to use any kind of Exception?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code "works", it compiles and does some IO. It is not correct still. You do not use any exceptions, you declare an exception will be thrown, but you never throw an exception. So when you turn your app in and the teacher flames it for not doing what was asked... know this: programmers each with decades of experience told you before hand that it was not working.

ekte spiriopoulos wrote:
Les Morgan wrote:ekte,
you have 2 things wrong with your code: . . .
he just say to do exceptions he doesnt care which one Anyway with this exception this worked .Thanks a lot and your advice
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please don't quote the whole of the preceding post; that simply makes the thread longer. I have removed most of the quote.

We have an FAQ entitled, “It Doesn't Work Is Useless”. Maybe we should have one called “It Works Is Useless”, too . As LM has said, simply getting code to run does not mean there is anything good about it.
I am surprised if your instructor has given you such vague advice. You appear to have been told to create an InvalidNumberException. You were given some (I think not good) advice about how to create that. I suggested you do some looking around to find other Exceptions. Did you look through the tutorial which somebody gave you a link to? Now you appear to be using IOExceptions instead. Why? How did you get your teacher to tell you to use any kind of Exception?
sorry i dont understand well what you want to tell me .I will start make better to do questions to understand things i have read exceptions and from book but maybe i have blanks in my head.So to confern some things.
I hope you will fill this blank by answer me this question so i will try to create that with code(if you want ).
1) I see from books and from links internet that exception replace ALL exceptions i mean arithmetik each.. is replace all if i use exception with nothing on it and there is no issue ,is this right?

2)i have a main ,the exceptions can i create inside in another class and to be inside in a method?After i can call it on my main(but there i will not have the try{}catch{} because i have it inside the method.I am right?

3)if i dont use this exception and i want a name lets say an exception like that --> IndexOutOfBoundsException .I have a main class . I have a class Axn which i have this method and the name of my method is static void dAr(int a[]){ //here i have the try catch and inside the catch i have the name IndexOutOfBoundsException e and after a message with system.out}.Should i have the name static void dAr(int a[]) throws IndexOutOfBoundsException?or as i had previous?

4) Can i give on exceptions whatever name i want?this is right too?

hint :Sorry guys but exceptions is difficults for me .I read but i dont understand well.





this is good right?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:. . .
1) I see from books and from links internet that exception replace ALL exceptions i mean arithmetik each.. is replace all if i use exception with nothing on it and there is no issue ,is this right?
No. That would only work in a simple case where you require the same response for all types of Exception. You would want a different response for an invalid number exception from an IO exception. In one case you might provide a new input, and in the other case you might have to check a network connection.
2)i have a main ,the exceptions can i create inside in another class and to be inside in a method?After i can call it on my main(but there i will not have the try{}catch{} because i have it inside the method.I am right?
you should probably not handle any exceptions in the main method; if an exception reaches the main method there is probably nothing you can do about it and you might as well simply propagate it. That assumes you have got the main method to a suitable size: one statement. You should handle all exceptions somewhere else. You should never throw an Exception and handle it inside the same method.
3)if i dont use this exception and i want a name lets say an exception like that --> IndexOutOfBoundsException .I have a main class . I have a class Axn which i have this method and the name of my method is static void dAr(int a[]){ //here i have the try catch and inside the catch i have the name IndexOutOfBoundsException e and after a message with system.out}.Should i have the name static void dAr(int a[]) throws IndexOutOfBoundsException?or as i had previous?
That question is really confusing because I think you have put all sorts of extraneous things in it, but I think it means, should you declare IndexOutOfBoundsException with a throws clause?
  • 1: Please read the documentation for IndexOutOfBoundsException and look carefully at its inheritance hierarchy.
  • 2: Now read this Java™ Tutorials section and see what it says. You should be able to work out whether you should or should not write throws about that sort of exception.
  • 4) Can i give on exceptions whatever name i want?this is right too?
    You should always call an Exception class XYZException or ABCException or similar. The name should make it clear both that it is an Exception and also why it has occurred.
    . . . this is good right?
    Afraid not. You are declaring Exception which your method does not appear to throw at all. You are catching an Exception in the same place as you think it occurs. But I don't think that Exception can even occur there. The error message does not tell anybody what has done wrong. If you are the user, you will simply get a message
    Error ....
    …and not know what it means. If you are programming, you will not know which part of the programming to correct.
    You also have some poor formatting and the method name MRr looks incomprehensible to me.
     
    ekte spiriopoulos
    Ranch Hand
    Posts: 217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i understand enough thanks a lot because something is my mind i didnt catch it well i bring a simple example Am i right? or i have understand it wrong?
     
    ekte spiriopoulos
    Ranch Hand
    Posts: 217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ekte spiriopoulos wrote:i understand enough thanks a lot because something is my mind i didnt catch it well i bring a simple example Am i right? or i have understand it wrong?


    what am i doing false :/ ?
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Throwing the wrong Exception. You should throw an illegal argument exception; you should also explain in your documentation comments that only values ≥ 2 and ≤ 60 are permitted.
    Once you throw that exception you should not catch it in the same place. Actually you should probably not catch that Exception at all.
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You also have a static method with th same name as a kind of Exception. That whole method is probably a mistake and should be removed.
     
    ekte spiriopoulos
    Ranch Hand
    Posts: 217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Throwing the wrong Exception. You should throw an illegal argument exception; you should also explain in your documentation comments that only values ≥ 2 and ≤ 60 are permitted.
    Once you throw that exception you should not catch it in the same place. Actually you should probably not catch that Exception at all.
    So this arithmetic method i must replace it and to be in a different place(you mean class? )
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Throw a different Exception and do not catch it in the same method. When the Exception passes on to the other method, you will have to think what you can do about it there. If you get a complaint that the value was < 2 or > 60, can you do anything about it in that other method? Only you can work that out.
     
    ekte spiriopoulos
    Ranch Hand
    Posts: 217
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Throw a different Exception and do not catch it in the same method. When the Exception passes on to the other method, you will have to think what you can do about it there. If you get a complaint that the value was < 2 or > 60, can you do anything about it in that other method? Only you can work that out.
    i delete everything i start again because i lost it ..this i have it in main on the class in vehicle i have this 2 methods (one method and one method for exception) : hint : i am terrible wrong .The exception is the most difficult lesson ever .I have lost it at all.If it could be easy i want the steps for solve it ,example : 1) create a class 2) inside this create a method ..Something like that ,because i stuck i dont know what i am doing if is right or wrong
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!