Creating your own exception classes Assignment Help

Assignment Help: >> Exception handling in Java >> Creating your own exception classes

Creating your own exception classes:

Even though the Java class library defines a large number of exceptions, there may be times when you want to define your own exceptions that correspond to error conditions that are specific to a particular application. Since exceptions are just objects, all you need do is create a new class that inherits from one of the exception classes that are provided as part of the Java class library (normally Exception or a checkable subclass of Exception).

For example, if we wanted to have a more specific exception type for the file format error above, we could define a new exception type like this:

class FileFormatException extends IOException
{
private int errorPosition;

public FileFormatException () {}

public FileFormatException (String message, int bytesRead)
{
super(message);
errorPosition = bytesRead;
}

public int getErrorPosition ()
{
return errorPosition;
}
}

Most exception classes have two constructors - a constructor with no arguments, and a one-argument constructor that allows for a more detailed error message. In this case, we have a constructor with no arguments and one with two arguments. This makes it possible to store and retrieve more detailed information on the location of the error within the file.

This exception can then be used within code in the same way that standard Java exceptions can be used. For example, in a method header:

public boolean checkFormat (String fileName)
throws FileFormatException
Our new exception can also be used in a try-catch statement:

try
{
// code to be checked
if (checkFormat(theNewFile))
{
...
}
}
catch (FileFormatException exception)
{
String reason = exception.getMessage();
int errLoc = exception.getErrorPosition(); System.out.println(reason+" at "+errLoc);
}

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Creating your own exception classes homework and assignments? Live Creating your own exception classes experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Creating your own exception classes homework help, java assignment help and Creating your own exception classes projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd