Solved

Need Help w/ Exception handler

Posted on 2006-11-22
7
1,219 Views
Last Modified: 2012-05-05
Hey everyone here is my assignment for an online class im taking

Write a program that shows a constructor passing information about constructor failure to an exception handler.  Define class SomeException, which throws an Exception in the constructor.  You program should try to create an object of type SomeException, and catch the exception that is thrown from the constructor.  

I wanted to get some input on my code to know if im following the assignment spec, it is hard to learn straight from abook w/o any input if you know what i mean.

class SomeClass
{

    public SomeClass()
        throws Exception
    {
        throw new Exception();
    }
}



import java.io.PrintStream;

public class Demo3
{

    public Demo3()
    {
    }

 public static void main(String args[])
 {
        SomeClass someclass;
        try
        {
            someclass = new SomeClass();
        }
        catch(Exception exception)
        {
            System.out.println(exception.toString());
        }
    }
0
Comment
Question by:imlearning
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:ADSLMark
ID: 17997758
Looks ok, I only think that SomeClass should be SomeException.

I agree with you that the specification is a bit vague, but it doesn't sound all too difficult. Seems like you got it right.

Mark
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 100 total points
ID: 17997824
Yes you forgot to name it right, but this homework (!) concerns the transmission of info.

      throw new Exception();

should therefore be:

      throw new Exception("Some thing went wrong in the constructor.");

To prove that it works, this message should appear in:

          System.out.println(exception.getMessage());
 
 Otherwise you did not fulfil the task.

;JOOP!
0
 
LVL 6

Accepted Solution

by:
SamsonChung earned 300 total points
ID: 17998920
A note

this is OOP...

so take advantage of it.

import java.io.PrintStream;

public class Demo3
{

    public Demo3()
    {
    }

 public static void main(String args[])
 {
        SomeClass someclass;
        try
        {
            someclass = new SomeClass();
        }
        catch(Exception exception)
        {
            System.out.println(exception.toString());
        }
    }


should be recode to

import java.io.PrintStream;

public class Demo3{

    public Demo3(){
        SomeClass someclass;
        try{
            someclass = new SomeClass();
        } catch(Exception exception) {
            System.out.println(exception.toString());
        }
    }

  public static void main(String args[]){
    Demo3 yo = new Demo3();
  }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:ADSLMark
ID: 17998951
May i ask why?

Just saying, it's OOP and you should take advantage, does not say very much.

Mark
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 17999006
Mark,

It is a line say to me by my profs when I was an undergrad......

also,

this way, you can always ensure your java files are resuseable.

It is way better then coding everything in main.

That's OOP.....
0
 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 100 total points
ID: 17999110
I agree, but i do not see the advantage in this example. It is a very small piece of code, where the main is more used as a test method then a really complicated application.

Anyway, i think we are on the same level here, so no need to discuss this, i was just wondering what was the big advantage in this example.

Mark
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 17999197
Ah..

Mark, I guess is just that T.A. in me... he never left the education sector.... lol

anyways, thanks.

and imlearning, hope you learn Java happily. It is a fun language to code in... especially how much it can actually do.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question