Exceptions

How are you today folks? fine right? well i’m fine as well. Last time we talked about casting, which is  a method that will take an object of one type and assign it to a reference variable of another type, for example a parent class want to initialize and use the function of an object in the child class. Today we’ll talk about exceptions or rather called error in general.

Have you ever given an exeptions in your life? majority of you will definitely say yes, and for example : You are forbidden to use your phone in class, but… you are permitted if there is an emergency call from your parent or your in an emergency situatuin, that is how an exeption would be in real life but now let’s talk about Java exceptions.
Exceptions in java means, a problem that show up in the middle of a program execution. When an exception occurs your program flow will be interupted or disrupted and you program will terminate abnormally, of course all of us don’t like this so excetions is to be handled.

Now let me give you an example for when an exception will show up :

  • User input invalid Data (you put String data types for Integer variables)
  • A file that needs to be run in order to complete the program is lost or not found
  • A lost connection in the middle of a somekind of communication or the Java Virtual Machine has run out of memory

There are 2 types of Exeptions :

First :
Its called as Checked exceptions, a checked exception is an exeptions that will occurs at the compile time of a program, checked exceptions are also called compile time exceptions. These kind of exceptions must not simply ignored at a time compilation, the user or programmer should take care of (handle) these kind of exeption.
For example, when you use a FileReader class inside you program to read data from file outside the IDE, if the file that is specified doesn’t exist an exception will pop up which is the FileNotFoundException, and the programmer should and must handle with the exception, if not their program will not run.

Example :

import java.io.File;
import java.io.FileReader;

public class exmpl {

public static void main(String args[]){
File file=new File("E://newFile.txt");
FileReader fr = new FileReader(file);
}

}

it will output this exception :

C:>javac exmpl.java
exmpl.java:8: error: unreported exception FileNotFoundException; must be caught or 
declared to be thrown
FileReader fr = new FileReader(file);
^
1 error

Second:
Its called as Unchecked exceptions, which means that this exceptions will occurs at the time of program execution, Unchecked exceptions are also called as Runtime Exceptions, these kind of exceptions includes trouble like bugs, logic error or improper uses of an API runtime exceptions are ignored at time of compilation. Example, if you declared an array size of 5 in your program, and your calling the 6th element of the array then ArrayIndexOutOfBoundsExceptionexception occurs.

 Example :

public class exmpl {

public static void main(String args[]){
int num[]={1,2,3,4};
System.out.println(num[5]);
}

}

it will output this exception :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at
Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)  

So now that you know what exception is and what invokes it, I hope you get better at handling exception and make you code/program better.


Conclusions

  • Exceptions in java means, a problem that show up in the middle of a program execution. When an exception occurs your program flow will be interupted or disrupted and you program will terminate abnormally.
  • A scenario example of when an exception occurs :
    • User input invalid Data (you put String data types for Integer variables)
    • A file that needs to be run in order to complete the program is lost or not found
    • A lost connection in the middle of a somekind of communication or the Java Virtual Machine has run out of memory
  • Checked exception is an exeptions that will occurs at the compile time of a program.
  • Checked exceptions are also called compile time exceptions.
  • Unchecked exceptions will occurs at the time of program runtime execution.
  •  Checked exceptions are also called compile time exceptions.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

One thought on “Exceptions”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.