• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Various examples of having errors thrown relative to the Error class

 
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Error class is the superclass for over 30 classes; http://docs.oracle.com/javase/7/docs/api/

I though it would be nice to create a thread that highlighted how different errors can be produced.

I'm starting this thread with the easy OutOfMemoryError condition. If anyone wants to add examples for different types of errors, go for it!

Error produced: OutOfMemoryError
Error message seen: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Code to produce error:
 
Greenhorn
Posts: 1
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I have tried to create java.lang.AbstractMethodError from table 9-4 as mentioned in the exercise.
Here's how i was able to create the error:


here's the code:
#1 created and compiled an interface

#2 Implemented that interface and compiled it.


#3 Then changed interface to have a new method and compiled; didn't compile inplementing class.


#4 created a class with main method, initialized class and called new interface method.



I was able to successfully catch the exception.

Thanks for a great book.
 
Ranch Hand
Posts: 112
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forced Assertion Error!!!

Whenever using assertions you need to make them visible by sending the -ea flag to the command-line.
And, the -ea flag needs to come directly after the java command.
This took me a long time to determine the problem! In the end there was no problem at all. I had placed the -ea flag after the class name and the JVM was not recognizing the flag.

This is what the incorrect command looked like:


This is what the successful command looked like:


Here is the code I wrote to force the assertion error:


Finally, here is the error from my command prompt!


Thank you Mr. Liguori and thank you to your co-author Mr. Finegan! This book was exactly what I needed.
I enjoyed the exercises that told me to go to the Java API or to do my own thing. The open ended question model, put with some of the more structured questions, stimulated my critical thinking about how to answer the question.

I will be finished with Chapter 10 by Saturday and plan to take the exam next week!
 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StackOverflowError, which is quite easy to recreate. Just think of a recursive function which creates a huge stack in the VM, and it'll ultimately crash. I've did it with the Fibonacci sequence, with Integer.MAX_VALUE.

 
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have chosen the NoClassDefFoundError, I used the exercise that I had to do a personal exception to do this.
I have a MainClass.java and a MiException.java and I compile it.


javac MiException.java MainClass.java


Before I run the program I do a dir (I am running in windows), and I get:


27/03/2013 09:08 <DIR> .
27/03/2013 09:08 <DIR> ..
27/03/2013 09:08 704 MainClass.class
27/03/2013 09:05 391 MainClass.java
27/03/2013 09:08 372 MiException.class
27/03/2013 08:30 255 MiException.java
4 archivos 1.722 bytes
2 dirs 183.944.970.240 bytes libres


I am going to delete the MiException.class file.


del MiException.class


and then i run the program.


java MainClass


Then i get my exception


Exception in thread "main" java.lang.NoClassDefFoundError: MainClass (wrong name
: com/ocaj/exam/tutorial/MainClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)


Thanks for the book.
 
Greenhorn
Posts: 1
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 1
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOError
Thrown when a serious I/O error has occurred

Issue: I wanted to upload my ascii input file mentioned below, but this web app prohibits it
Question: can someone let me know how to do it (if it's possible)?



1Z0-803 OCA Java SE 7 Study Guide
Ch. 9, p. 364a, Exercise 9-5
Section 8, Item 5
Recognize common exception classes and categories
Illustrates: [difficulty in] creating an error condition


This class (Exercise9_5) tries to force an IOError through the following steps.
  • It opens a connection to the input file stored on a thumb drive.
  • It enters a slow loop to read a character at a time from the file.
  • During the slow loop, I pull the thumb drive on which the input file resides.
  • I expect the sudden abscence of the thumb drive to produce an IOError.

  • Result: I could not force an IOError. However, I did empirically verify the default buffer size.


    Use this directory structure to build with the command lines I supply below.
    (Dir structure from DOS command "tree /a ." - I had to substitute dots for spaces to get the proper alignment)
    Q:\
    +---src
    |...+---main
    |.......+---java
    \---target
    ....+---classes
    ....+---generated-docs

  • Put the source in Q:\src\main\java\Exercise9_5.java
  • Put the input file on a thumb drive in something like F:\Exercise9_5.txt

  • To compile this source file ...
    Q:\src\main\java>javac -d ..\..\..\target\classes Exercise9_5.java
    To run the compiled byte code ...
    Q:\src\main\java>java -cp ..\..\..\target\classes Exercise9_5
    To create the javadocs for this class ...
    Q:\src\main\java>javadoc -d ..\..\..\target\generated-docs -private Exercise9_5.java


    Some comments:
  • Sorry I couldn't actually produce the IOError. I'm still trying different ways to force one ... will post it when I find it.
  • I thought others might benefit from seeing my employment of the book's lessons in my code.
  • I spent a lot of time figuring out how to "javadoc" my code. Others might benefit from what I learned by looking at the example docs in my code.

  • 1Z0-803 preparation summary
  • I began the book 13 Feb 2013 ... reached this example 26 Apr 2013
  • Spent 1 hour studying in the morning on average 4 days a week.
  • 2 to 3 hours studying in the evening on average 2 nights a week.
  • Spent 6 to 8 hours studying on weekends on average
  • My goal was to practice coding as much as possible, to the point where I can compile in my head.
  • Toward my goal, I typed in, compiled, corrected (when necessary), and ran every example in the book.

  • Result
    I feel confident about taking the test
    Will practice with Enthuware starting in a day or 2.
    This book is a valuable learning tool.

    Finally, here's my code
     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Easy one.

    Error produced: java.lang.ExceptionInInitializerError
    Error message seen:



    Code to produce error:

     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    package javaapplication7;

    public class JavaApplication7 {
    static {
    throw new NoSuchMethodException(nsme);
    }
    public static void main(String[] args) {
    }
    }


    java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code - initializer must be able to complete normally
    at javaapplication7.JavaApplication7.<clinit>(JavaApplication7.java:13)
    Exception in thread "main" Java Result: 1

    Thanks for a wonderful book.
     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Tried to get a OutOfMemoryError..



    Resulting in:
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at helloworld.Thrower.<init>(Thrower.java:14)
    at helloworld.Thrower.main(Thrower.java:8)
     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Easy OutOfMemoryError:


    Resulting in:
    java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
    at java.util.ArrayList.ensureCapacity(ArrayList.java:180)
    at ocajavase1.OutOfMemoryExample.main(OutOfMemoryExample.java:22)


    ER.
     
    Greenhorn
    Posts: 12
    1
    Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi there,
    As part of Exercise 9-5, I am posting up a simple NoClassDefFoundError:


    In this code I declared that the HelloPeople file was located in the package spoon, but I didn't put it into a spoon directory.
    It compiled fine but threw me this error:
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloPeople (wrong name: spoon/HelloPeople)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    I have created a small class that results in a StackOverflowError:

     
    Bartender
    Posts: 2237
    63
    IntelliJ IDE Firefox Browser Spring Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch!
     
    Greenhorn
    Posts: 1
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Every one, i'm so glad to join you.
    Actually i'm coming form OCA 7 Study Guide, especially Chapter 9.

    And i'm going to apply to kinds of error which is IllegalAccessError and NoSuchMethodError.

    here is the code:

    First.java


    Cmain.java including main method:


    The result would be:


    Hello From First.!
    5
    Sultan



    Now, to get IllegalAccessError, put num variable private in First.java and compile it alone without compiling Cmain.java:


    if you run the Cmain class the result would be:


    Hello From First.!
    Exception in thread "main" java.lang.IllegalAccessError: tried to access field com.sultan.data.First.num from class com.sultan.main.Cmain
           at com.sultan.main.Cmain.main(Cmain.java:9)



    regarding to NoSuchMethodError, remove getName() method form First.java and compile it alone again, Make sure to put num variable public to prevent IllegalAccessError:

    and the result would be:


    Hello From First.!
    5
    Exception in thread "main" java.lang.NoSuchMethodError: com.sultan.data.First.getName()Ljava/lang/String;
           at com.sultan.main.Cmain.main(Cmain.java:10)



    Thank you Mr.Finegan and Mr.Liguori for this book.

    Best regards
     
    Greenhorn
    Posts: 1
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,
    I have created UnsupportedClassVersionError.


    First, I create simple class, and compile with java version 8, default as JAVA_HOME Environment Variables


    javac  com/jscorp/test/Main.java  (Java 8)
    Then copy and paste the directory where the java 7 version is, C:\Program Files\Java\jdk1.7.0_80\bin and run program.

    C:\Program Files\Java\jdk1.7.0_80\bin>java com.jscorp.test.Main
    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/jscorp/test/Main : Unsupported major.minor version 52.0
           at java.lang.ClassLoader.defineClass1(Native Method)
           at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
           at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
           at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
           at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
           at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
           at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
           at java.security.AccessController.doPrivileged(Native Method)
           at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
           at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
           at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
           at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
           at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)






     
    Rancher
    Posts: 163
    5
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ThreadDeath Error

     
    Marshal
    Posts: 79978
    397
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ThreadDeath is weird. It says it is thrown when Thread#stop() is invoked, but you doubtless know you mustn't use Thread#stop() in real life. Older versions of the API said it is thrown when a thread terminates. Since threads often terminate, that is a normal occurrence. You shouldn't catch Errors in normal code, so that is a fiddle akin to having assertion error extend Error. It's explained in that API link. If you ever catch Errors, you should therefore rethrow thread deaths:-[addition]The thread death object is required by the JVM to enable it to close down the dying thread properly.
     
    We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic