Solved

Need Help w/ Exception handler

Posted on 2006-11-22
7
1,213 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 18
servlet example 17 53
Why doesn't this text field show up on my Applet frame? 2 20
swing controls 2 16
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

820 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