?
Solved

Need Help w/ Exception handler

Posted on 2006-11-22
7
Medium Priority
?
1,239 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 400 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 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