This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions -A class that already exist and appear a message  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Message: There is an exception.All is false
My main :
my class :
My class IllegalArgumentException is right? On my main i know that i i did wrong with try catch
 
Shaun Wild
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not 100% sure what you're asking but I'm going to make some assumptions...

You want to throw an exception if ?

If so you need only do:



try-catch statements are used for catching thrown exceptions - not for throwing exceptions!
 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaun Wild wrote:I'm not 100% sure what you're asking but I'm going to make some assumptions...

You want to throw an exception if ?

If so you need only do:



try-catch statements are used for catching thrown exceptions - not for throwing exceptions!
you solve it thank you.If it is not problem when i should use try-catch any example? on this exercise to understand it i will appreciate it
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is already an IllegalArgumentException class, that is part of the core java.lang package. It is probably not a good idea to implement your own, as it can get very confusing, when reviewing your code.

Henry
 
Shaun Wild
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ekte spiriopoulos wrote:
Shaun Wild wrote:I'm not 100% sure what you're asking but I'm going to make some assumptions...

You want to throw an exception if ?

If so you need only do:



try-catch statements are used for catching thrown exceptions - not for throwing exceptions!
you solve it thank you.If it is not problem when i should use try-catch any example? on this exercise to understand it i will appreciate it


When to use try- catch statments...

If a method throws an exception (specified with the throws keyword), you must either encapsulate any calls to that method in a try- catch or ensure that the method that is calling it throws the same exception. For example, in Java, Thread.sleep(long ms) is a function that throws an InterruptedException. So to call it you must encapsulate it in a try- catch as follows:



You can also make sure taht the method calling it throws the same exception too:



Just bare in mind that any method calling sleep(ms) would have to encapsulate sleep in a try- catch.

 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. You should try to work out what the Exception means, and what you are going to do if one is thrown. When you have worked out what to do about it, you can use a try-catch. If you do not think the current method is the best place for handling that Exception, you should notify any other methods using it that you cannot handle such an Exception. You do that by declaring throws InterruptedException after the method parameters
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!