• 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

Sybex 815 - potential error - chapter 1

 
Greenhorn
Posts: 7
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
book: Oracle Certified Professional Java SE 11 Programmer I Study Guide Exam 1Z0-815

chapter: 1

If a main() method isn’t present in the class we name with the .java executable, the process will throw an error and terminate. Even if a main() method is present, Java will throw an exception if it isn’t static.



but if the main method is not marked as static Java will also throw an error, not an exception

method main non-static:
Error: Main method is not static in class Main, please define the main method as:
  public static void main(String[] args)
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed that whether the main() method is completely missing, has the wrong parameters, or is not marked static, you get the Error:
Error: Main method not found in class Eraseme, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


If you try to return a value from it, because you are used to C/C++, you will get:

Error: Main method must return a value of type void in class Eraseme, please
define the main method as:
  public static void main(String[] args)


Still an Error, not an Exception.

It doesn't get capitalized if we use the newer run-straight-from-source approach:

PS F:\Java> java Eraseme.java
error: can't find main(String[]) method in class: Eraseme

PS F:\Java> java Eraseme.java
error: 'main' method is not declared with a return type of 'void'

PS F:\Java> java Eraseme.java
error: 'main' method is not declared 'public static'


But in all cases is indeed an error.

The exams are super-tricky, and it can be very annoying.

There is a lot of focus on whether something will fail to compile or throw an Exception or Error at runtime.

I think we are good for the exam in this particular case knowing that it will compile but fail to run.

You definitely want to know all the difference between Throwable, Error and Exception, as well as being able to clearly distinguish the difference in rules for handling sub-classes of RuntimeException versus sub-classes of Exception that are not sub-classes of RuntimeException for the exam.

This shows a good eye for detail, but I think of all the places where whether something is an Error or an Exception makes any difference, this one is least likely to show on an exam.  You'll see plenty more that make some more difference thru-out these long books.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically it is right. A process (in UNIX for example) doesn't know what an exception is. But I understand your confusion.
 
Jesse Silverman
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP's point was that if what the JVM throws in one case is an Error, the other very similar case wouldn't be expected to be throwing an Exception.

I believe I agreed.  It seems more than likely that either both cases described throw type Error or that both throw type Exception, the wording in the book subtly suggests otherwise.

There is no place to put a catch { } to check, I am not sure how else to confirm it.

I looked at 12.1.4 in the Java SE 17 JLS.  It states the requirements to run successfully, but not what type gets thrown if they aren't met.
 
Master Rancher
Posts: 4806
72
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we're taking a strict approach, I actually think it's a mistake to even talk about throwing an Error or Exception here.  There is no stack trace at all - execution hasn't even begun, of any Java code at least.  You can't even get output from a static initializer, which would normally give you output before the main() method starts:

So I would say there is no Error or Exception or any Throwable, in the Java sense of the term.  There's an error message, which may be capitalized or not, depending how you run the program.  But that just depends on how the person who wrote the message chose to format the message.  I don't see any indication any java.lang.Throwable exists here.  There's nothing to throw, and nothing to catch.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic