The exception is a term in applications development or programming. Simply exception is used to specify a state or a situation which is unexpected in applications. Exceptions occur in different and unexpected situations where there are a lot of different cases for an application and algorithm. The term also throwing exception
by and applications also used for occurrences of the exceptions.
Exceptions occur during runtime which means exceptions occurs during usage of the applications. This is different then compile-time errors which will simply prevent the applications compile and program and binary creation. This runtime behavior makes exceptions unexpected and problematic to the applications developers because generally they can not be defined during application development.
“Raising, Throwing and Handling An Exception” Terms
There is 3 main term used with the exception which raising exception
, throwing exception
and handling exception
.
- `Throwing Exception` means there is a case where an exception will be thrown by the application without the intent of the application. This occurs if the uncalculated sitıations and not an explicit exception creation by the applications or application developer.
- `Raising Exception` means there is a programmer-defined exception situation and the exception should be raised explicitly. This type of exception cases is precalculated and coded into applications.
- `Handling Exception` means managing the exception with different actions like ignoring it, logging it, taking another program into action. Exception handling is important which is defined for expected exception locations and handle the exception with different actions inside the program.
Example Exception Cases
There is a lot of different type of exceptional cases. But some of them are very popular and faced a lot of time while applications development. Generally, exceptions are named with the library or class name of exception but in order to be simple and understandable, we will list them with the case name.
Dividing By Zero
Dividing by zero
is one of the most popular exceptions. This is actually a mathematical exception which is not logical. We can not divide any number into 0 because of its unlogical and equals to the infinite. This exception generally categorized under ArithmeticException
in programming languages.
Access To Undefined Item In Array
Another popular exception type is trying to access or call or an unexisting item in an array. This exception is generally related with the using an index number which item does not exist. Think about we have an array named cars
with 5 items but we call for 7th item which does not exist in cars arrays and will raise an exception.
car[5]={"hyundai" , "fiat" , "ferrari" , "skoda" , "renault"} mycar = cars[7] //This will raise an exception
Convert Incompatible Types
Programming languages provide some short cuts to the programmers and developers like converting some type into another type without writing excessive code. For example, we can convert “45.3” string type into a floating number type. But is the string contains non-numeric characters like “a”,”!” etc this will raise an exception named Convert Incompatible Type
.
mystringnumber="12.34abc" mynumber = Convert(mystringnumber) //will raise an exception
Call To Non-Existing Object
Another exception type which occurs generally is Call To Non-Existing Object
. This generally occurs when we call an unexisting object or function which is not properly initialized or created. For example Object.create() function does not return an object to the variable myobj and the usage of obj will rais an exception.
myobj = Object.create() myobjec.somemethod() // will raise an exception
Exceptions In Java
Java programming language provides try
, catch
, throw
keywords in order to manage exceptions.
- `try` is used to surround possible exception code block where there is a probability to occur an exception.
- `catch` is used to specify instruction or commands run when previously defined `try` or exception block throws an exception.
- `throw` simply used to raise an exception explicitly or manually.
User-Defined Exception
We can also create user-defined exceptions which are designed for special cases. The base class for a user-defined exception is Exception
which will be extended. In this example, we will create an exception named PofException
which will raised in our main application.
// This is a user defined exception class class PofException extends Exception { public PofException(String s) { // Call constructor of parent Exception super(s); } } //Program that will create or raise exception public class Main { // Driver Program public static void main(String args[]) { try { // Throw an object of user defined exception throw new PofException("Poftut.com"); } catch (PofException ex) { System.out.println("Exception Caught"); // Print the message from MyException object System.out.println(ex.getMessage()); } } }