Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1262
  • Last Modified:

Need Help w/ Exception handler

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
imlearning
Asked:
imlearning
  • 3
  • 3
3 Solutions
 
ADSLMarkCommented:
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
 
sciuriwareCommented:
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
 
SamsonChungCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ADSLMarkCommented:
May i ask why?

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

Mark
0
 
SamsonChungCommented:
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
 
ADSLMarkCommented:
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
 
SamsonChungCommented:
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now