aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Various examples of having errors thrown relative to the Error class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Various examples of having errors thrown relative to the Error class" Watch "Various examples of having errors thrown relative to the Error class" New topic
Author

Various examples of having errors thrown relative to the Error class

Robert James Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 532
    
    5

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:


My Blog (I frequently post on Java, JSF and NetBeans)
Rohtak Dev
Greenhorn

Joined: Feb 17, 2013
Posts: 1
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.
Kent O. Johnson
Ranch Hand

Joined: Feb 21, 2013
Posts: 32
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!
Geroen Joris
Greenhorn

Joined: Feb 20, 2013
Posts: 4
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.

Eduardo Ucha
Greenhorn

Joined: Mar 27, 2013
Posts: 1
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.
Tom McGovern
Greenhorn

Joined: Apr 25, 2013
Posts: 1
Mike Rocha
Greenhorn

Joined: Apr 26, 2013
Posts: 1
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
    Arnaldo Filho
    Greenhorn

    Joined: Jul 03, 2013
    Posts: 1
    Easy one.

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



    Code to produce error:

    Andrii Melnyk
    Greenhorn

    Joined: Jul 23, 2013
    Posts: 1
    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.
    Div Ander
    Greenhorn

    Joined: Jul 29, 2013
    Posts: 1
    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)
    Eduardo Rosales
    Greenhorn

    Joined: Oct 06, 2013
    Posts: 1
    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.
    Vivienne Ryan
    Greenhorn

    Joined: Dec 12, 2013
    Posts: 12

    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)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Various examples of having errors thrown relative to the Error class