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.
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:

Do the following using JAVA language. a) Create an Error. (You can create any error.) b)...
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 CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...
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 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 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( ) 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...
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...
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...
Java programming: Identify the true statements. Select ALL that appiy. A.Only one catch block can follow a try block. B.Class throwable is| the parent class for all exceptions C.You can create your own exception classes by extending class Exception. D. ArithmaticException is a checked excetpion
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.