File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Reply 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 Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 426
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:


Robert J. Liguori - OCA Java SE 7 Programmer I Study Guide (Exam 1Z0-803)
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.
Ortell Johnson
Greenhorn

Joined: Feb 21, 2013
Posts: 4
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
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Various examples of having errors thrown relative to the Error class
     
    Similar Threads
    generics vs arrays
    ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>();
    Recursion, with using Lists to store data