Solved

Need Help w/ Exception handler

Posted on 2006-11-22
7
1,200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:ADSLMark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 83
java set up 1 44
firstChar challenge 13 82
starter POM and spring-boot-starter,  spring-boot-web 2 9
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now