Смарт контракты

Published 15 October 2018

Exceptional situations in Java and their processing

What is an exception in Java, the keywords used in exceptions (try, catch, throw, throws, finally)

What is an exception in Java, the keywords used in exceptions (try, catch, throw, throws, finally).

Exceptions are errors and other possible problems that may arise during the execution of the program, leading to the impossibility of further development of the algorithm by the program.

The Java programming language has a mechanism for handling exceptional situations, it simplifies the creation of reliable programs and increases the confidence that there will be no unprocessed errors in the application that can disable the program as a result of its execution.

Exception handling in Java is based on the use of the following keywords: try, catch, finally, throw, throws.

he construct of the exception handling block (try, catch, finally), how it works

The construct of the exception handling block (try, catch, finally), how it works

try {

// code block where errors are tracked

}

catch (exception_type_1 exceptionObject) {

// handling the error

}

catch (exception_type_2 exceptionObject) {

// handling the error

}

finally {

// the code to be executed after the try block is finished

}

During the development phase of the program, dangerous code fragments are limited against exceptions by using a try{} block, if an exception occurred, it can be intercepted and processed using a catch{} block, and inside the finally{} block you write the code that is executed in the program regardless of whether any errors were caught.

Java provides a class for handling exceptional situations -  Trowable. It includes two classes, Exception and Error. The Exception class is used to handle exceptions by the program, and you must use the inheritance mechanism to create your own exception types. There is a RuntimeException class for common errors. The Error class is used to handle errors in the Java programming language itself.

Throw, throws statement

Throw, throws statement

The throw statement allows you to create your own exception type and process it manually in program code. To create own exceptions, you need to create an instance of Throwable class:

throw instance_Throwable

Manual handling of the own exception type is performed using the blocks to process exceptions try{} catch (type_of_own_exception_1 exceptionObject){} finally{}.

The generated and passed exception within a method must be specified in its interface by the throws statement.

The construct of declaring the method with a throws statement:

type method_name(parameters_list) throws exceptions_list {

// code inside the method }

Built-in Java exceptions

Built-in Java exceptions

Java exceptions listed below are the most commonly found ones:

  • ArithmeticException - errors associated with arithmetic operations such as division by zero;
  • ArrayIndexOutOfBoundsException – access to the array element that is out of its bounds;
  • ArrayStoreException - assigning an object of an incompatible type to an array element;
  • ClassCastException - wrong casting of one type to another;
  • EnumConstantNotPresentException - an attempt to use an undefined enumeration value;
  • IllegalArgumentException – using the wrong argument when calling a method;
  • IllegalStateException - incorrect state of the application;
  • IllegalThreadStateException - the requested operation is incompatible with the current thread;
  • IndexOutofBoundsException - the type of the index went beyond the allowed limits;
  • NegativeArraySizeException -  an array of negative size was created;
  • NullPointerException - incorrect use of null reference;
  • NumberFormatException - incorrect conversion of a string into a number format;
  • StringIndexOutOfBounds - an attempt to use an index outside the string;
  • TypeNotPresentException - type not found;
  • UnsupportedOperationException - unsupported operation detected;

Exception handling rules

Exception handling rules

  • Do not use the Exception class to catch exceptions;
  • correct the problem (error) and call the method that initiated the exception again;
  • catch exceptions only when you can effectively process them;
  • by intercepting an exception, close the open resources;
  • use your exception types, it will allow you to feel the program better, to work with it more effectively;
  • write the messages provided by the exception to the log file;
  • document the exceptions in javadoc.
Votes 0, average rating 0

Contents