Class Exception. Create custom exception classes. Examples
Contents
- 1. The purpose of the class Exception. Creating your own exception subclasses
- 2. An example of creating a Triangle class. Calculating the area of a triangle using TriangleException class
- Related topics
Search other websites:
1. The purpose of the class Exception. Creating your own exception subclasses
The Exception class is the base class for all user classes in the program. In order to create a class of its own exception, it is enough to define it as a derivative of the Exception class. Since the Exception class is derived from the Throwable class, all methods from the Throwable class are available in the Exception class.
That is, when declaring a class MyExceptionClass like
class MyExceptionClass extends Exception { }
an instance of this class will be able to use all the methods of the Throwable class. The following code demonstrates the use of the getMessage() method from the Throwable class.
public class TrainException { public static void main(String[] args) { MyExceptionClass mc = new MyExceptionClass(); String str = mc.getMessage(); System.out.println("str = " + str); } }
The result of the main() function
str = null
So, in your own implemented subclasses that are inherited from the Exception class, you do not have to implement your own methods. Methods of base class Throwable are often sufficient.
⇑
2. An example of creating a Triangle class. Calculating the area of a triangle using TriangleException class
The example demonstrates how to create your own TriangleException class to handle the “incorrectly defined triangle sides” exception.
Two classes are declared:
- Triangle – implements a triangle on its sides a, b, c;
- TriangleException – implements the interception of the “incorrectly defined sides of the triangle” exception.
The text of the program is as follows:
import java.util.Scanner; // Create your own TriangleException exception class // Creating your own TriangleException exception class class TriangleException extends Exception { // override toString() method describing the exception public String toString() { return "Error. Bad sides of triangle."; } } // declare a class Triangle class Triangle { // sides of the triangle private double a,b,c; // default constructor public Triangle() { // equilateral triangle with side length 1 a = b = c = 1; } // parameterized constructor public Triangle(double _a, double _b, double _c) { // using the TriangleException exception class try { // Is it possible to create a triangle from the sides _a, _b, _c if (((_a+_b)<_c)||((_a+_c)<_b)||((_b+_c)<_a)) throw new TriangleException(); } catch(TriangleException e) { System.out.println("Exception: "+e.toString()); return; } // if the sides of the triangle are entered correctly, // write them to class internal variables a = _a; b = _b; c = _c; } // method returning the area of a triangle public double getArea() { // if the sides of the triangle have the correct dimensions, // then you don’t need to check the root of a negative number double p, s; p = (a+b+c)/2; // semiperimeter s = Math.sqrt(p*(p-a)*(p-b)*(p-c)); // Heron's formula return s; } } public class Train04 { // the main() function tests the operation of the Triangle and TriangleException classes public static void main(String[] args) { double a, b, c; Scanner in = new Scanner(System.in); // input a, b, c System.out.print("a = "); a = in.nextDouble(); System.out.print("b = "); b = in.nextDouble(); System.out.print("c = "); c = in.nextDouble(); // area calculation Triangle tr = new Triangle(a,b,c); double area = tr.getArea(); System.out.println("area = " + area); in.close(); } }
The TriangleException class is inherited from the Exception class, which in turn is inherited from the Throwable class. Thus, in the TriangleException class, methods of the Throwable class are available, including the toString() method that describes the exception. The TriangleException class overrides the toString() method of the Throwable class. This is done with the aim of making the description of the exception itself more specific.
The main() function uses the Scanner class from the java.util package to enter data. Therefore, at the beginning of the program, the following command is entered
import java.util.Scanner;
The result of the program if you enter incorrect data
a = 3 b = 1 c = 1 Exception: Error. Bad sides of triangle. area = 0.0
The result of the program if you enter the correct data
a = 2 b = 2 c = 2 area = 1.7320508075688772
⇑
Related topics
- Exceptions. Keywords try, catch, finally. Examples
- Operators throw, throws. Examples
- Java classes for handling exceptions from the java.lang package. Methods of Throwable class. Examples
⇑