Question

Do the following using JAVA language. a) Create an Error. (You can create any error.) b)...

Do the following using JAVA language.

a) Create an Error. (You can create any error.)
b) Handle the error.
c) Create a custom exception which will give a kind message.
d) Handle both of the errors by using only one try-catch block.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

public class MyClass {
public static void main(String args[]) {
int x=10;
int y=x/0;

System.out.println("Sum of y = " + y);
}
}

b)

public class MyClass {
public static void main(String args[]) {
int x=10;
try
{
int y=x/0;
System.out.println("Sum of y = " + y);
}
catch(ArithmeticException e)
{
e.printStackTrace();
System.out.println("Error handled");
  
}
}
}

Output:

c)

class CustomException extends Exception
{
public CustomException(String s)
{
super(s);
}
}
public class MyClass {
public static void main(String args[]) {
int x=10;
try
{
int y=x/2;
System.out.println("Sum of y = " + y);
throw new CustomException("Custom Exception");
}
catch(CustomException e)
{
e.printStackTrace();
System.out.println("Error handled");
  
}
}
}

Output:

d)

class CustomException extends Exception
{
public CustomException(String s)
{
super(s);
}
}
public class MyClass {
public static void main(String args[]) {
int x=10;
try
{
int y=x/0;
System.out.println("Sum of y = " + y);
throw new CustomException("Custom Exception");
  
}
catch(ArithmeticException | CustomException ex)
{
ex.printStackTrace();
System.out.println("Exception handled");
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Do the following using JAVA language. a) Create an Error. (You can create any error.) b)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed),...

    JAVA Question 11 (20 points) Using your choice of language, C# or Java (no pseudocode allowed), give a format/template of Exception Handling codes with try, catch, and finally blocks. In the try block include some statements that raise an arithmetic exception and the catch block catches that arithmetic exception. You don't need to write any complete method or program, just write the code segments with try, catch, and finally blocks. O Paragraph BI U Question 12 (2 points)

  • Java(Eclipse). The task will create a server and client and send a message from the client...

    Java(Eclipse). The task will create a server and client and send a message from the client to the server. We will have two classes NetworkClient and NetworkServerListener classes. These will each have a main to run from the command line The NetworkServerListener class will listen for a client connection from NetworkClient , receive a message, display the message to the console (stdout) and exit. The NetworkClient class will create a connection to the NetworkServerListener and send a message to the...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • Create a File menu in your GUI. Add a file menu to your clockGUI with options...

    Create a File menu in your GUI. Add a file menu to your clockGUI with options to open any file for reading ( and displaying the file as in Project2) and one to Quit the program.You will need a FileMeu Handler classto handle the events from FileMenu. Be sure to use getAbsolutepath() when getting the file from JFileChooser, not getName(). Handle Exceptions A data file will be provided that will contain errors (eg- hours>23,minutes>59, seconds>59). Create an exception called IllegalClockException...

  • Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...

    Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • Select a Java program that contains an exception error. The exception error can be one that...

    Select a Java program that contains an exception error. The exception error can be one that you have encountered yourself or one you located using the Internet. Describe your chosen exception error and explain potential implications. Be sure to include the actual code in your posting. Prior to submission, check your colleagues' postings, as your post must contain a different error message from your colleagues.

  • 3. Handling exceptions with a finally clause a. Download the following file from the class website:...

    3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT