• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exploring exceptions

 
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I want to do is have main call crossFingers.
crossFIngers calls takeRisk.
I want to see how the Exception gets handled.
I marked lines with errors that I don't understand.


 
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the scope of "getThatError" ?
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I know that an exception error is an object.  Would I need to write my own class for the BadException object?   Is this what defining BadException means?  

I know what lifetime and scope mean, but I don't know what the scope of getThatError is.   Maybe It stays on the stack or heap as long as long as main() is running which is until the code stops.

The Heads Up Book is showing me how to work with the getSequencer method in


and



Since I'm learning exceptions maybe I should work off of their example.  

Thanks,

Kevin

 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:

Class names should begin with an upper case letter. You have a local variable "getThatError" that is not accessible in crossFingers().

Yes, "BadException" (starting  with an upper case letter) is a class and Java doesn't come with this one so you have to write your own or change to use one of  Java's existing Exception classes, e.g. "Exception".
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 12, that's not how you'd call takeRisk(), it's a member method being called from another member method.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Having a variable with the exact name as a class is a problem when trying to read and understand code.
Class names should begin with an uppercase letter.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took as many pieces of improvement you each described and came up with this code:

My questions are where there are //.

Thanks,

Kevin



 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 13, "gte" is not accessible. It is a variable local to main(). And crossFingers() is a member method so it can call takeRisk(), another member method, with out any prefix needed, it will use an implicit "this.".

BadException should be a class not a method and it must extend Exception.
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In ChatGPT: "how to write a java exception class"

In Java, you can create your own custom exception classes by extending the built-in Exception class or one of its subclasses like RuntimeException. Here's a step-by-step guide on how to create a custom exception class:

   Choose a Descriptive Name: Decide on a meaningful name for your custom exception class that reflects the type of error or exceptional situation it represents.

   Extend an Existing Exception Class: Decide whether your custom exception should be a checked exception (directly extends Exception) or an unchecked exception (extends RuntimeException). If you want your custom exception to be a checked exception, extend the Exception class. For unchecked exceptions, extend RuntimeException.
Add Constructors: Define constructors for your custom exception class. Typically, you'll want to provide at least one constructor that takes a message string, which you can pass to the superclass constructor.
Optional: Additional Information: You can add additional information to your custom exception class, such as fields or methods that provide more context about the exception.
Usage: To use your custom exception, you can throw it within your code when an exceptional situation occurs. Here's an example of how to use your custom exception:
Remember that creating custom exceptions can improve the readability and maintainability of your code by providing meaningful error messages and contextual information when exceptional situations arise.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I want to get this code working.  (I took it from your post).  After that I want to write code to see how the exception object we created gets passed around between objects.

I created a package and put two classes in it.  

I don't know what is wrong with super(message).




i tried giving it the full path for howAboutExceptions.CustomCheckedException but it did not help.
It seems as if it wants an import statement to know how to use throws.  Is that possible?

 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where's the "extends" ?
 
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:

While this may happen to work in your example code, this is not normally how you should to compare Strings.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is working now.
I coded the String compare in a better way.
I keep manually tracing the code starting from the main() method.  I would need this as an example if I ever need to make my own Exception handler.
I wouldn't need the full path like this:    howAboutExceptions.CustomCheckedException
I needed the import.  I would have thought it is part of java.
I think that I'll go back to page 125 in the book and when I get up to page 440 where the Exception discussion starts I'll have some idea of how this works.
I need to stop starting sentences with the word I.  

 




 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I wouldn't need the full path like this:    howAboutExceptions.CustomCheckedException


You don't need the full path if they're both in the same package.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I found out that I don't need it by experimenting.  

Thanks,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that it is typical to create a hierarchy of Exceptions. SQLException is one  such example, it is the root of all SQL exceptions and the others are more specific and inherit from SQLException.

So, you might create an AbelException as the root and then create more specific exceptions that extend that.

This gives you the opportunity to design your try/catch code to either catch a specific exception or the generalized root.

As an example, database code often catches SQLException because it is not necessary to know which specific SQL exception was thrown.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I was reading about the hierarch  on around Page 450 in the HF Book.  I wanted to get a taste of what these Exception objects do.   I'm back at Page 133.

I have about 100 pages of topics I feel good about learning.  Then comes polymorphism which was scary.  

Thanks,

Kevin
 
Marshal
Posts: 79405
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I am late to the party, but I think you are making life difficult for yourself by trying to do too many things at once.
And getting yoiur IDE to create the structure of your classes will help you avoid some of the syntax errors.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I slowly made it from the start of the book up to page 133.  I'll ask questions as I move forward from that page.

Sometimes I feel like reading from Page 449.  I'll do that to get an overview but I won't do exercises or go too deeply into it.  

I'll probably break my rules a little bit.

Right now I'm struggling with getting some simple code working to learn about the ArrayList.  I have another thread going for that.  

Thanks,

Kevin
 
reply
    Bookmark Topic Watch Topic
  • New Topic