Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Exception Handling In Java
#1

Java 11 will be released in September of this year. Java 10 is the recent release now. Java Oracle team is working very hard to make Java the most powerful programming language. Java is already the most popular programming language with millions of programmers. Java has a huge demand in the market so if you learn Java for algorithmic trading, you can also use it to land a high paying job in the market. Java is being used in algorithmic trading a lot. I learned Java so that I could use it in algorithmic trading. But before you can use it in developing algorithmic trading strategies, you should master Java exception handling.

Exception occurs when the code you wrote faces a hurdle or a problem in its execution. Writing exception handling helps the application to resolve these exceptions and continue executing the code as if nothing has happened. So in java runtime errors are thrown as exceptions. Exception is an object in java that represents an error that is preventing the program from executing normally. If this exception is not handled, the program execution will be terminated abruptly. So this is important for you to understand. Exceptions are objects in Java and are defined using classes. The root class meaning the most basic class for exceptions is:

java.lang.Throwable

So Throwable is the root exception class meaning all other exception classes derive from this Throwable class using inheritance. We can also define our own exception classes by extending the Exception class or its subclass. Exception classes are of three types: 1)System Errors, 2) Exceptions and 3) Runtime Exceptions. System errors are thrown by JVM when there is some internal system error. System Errors are rare. But when they occur we can't do much about it. Only thing that we can do is terminate the program gracefully. Exceptions are handled by the Exception class as said earlier. Below are a few exceptions that you can encounter while developing a program:

ClassNotFoundException: Obvious when you try to run a class that is not there you will find this exception thrown.

IOException: This exception relates to input/output commands. You get an IOException FileNotFoundException when you try to open a file that is not there. When you try to read past the end of file, you get EOFException.

RuntimeException:  When you commit numeric mistakes in your code like diving by zero you get ArthimeticException. When you try to access an out of bound array index, you get IndexOutOfBoundException.

Error and RuntimeException are known as unchecked exception and you cannot deal with them using a try catch block code. Unchecked exceptions are due to logic errors in your code that you need to rectify. Java exception handling involves three steps comprising: declaring an exception, throwing an exception and catching an exception.

Declaring Exceptions
Java compiler invokes the main method and starts the program execution. Java does not require you to declare unchecked exceptions that are the runtime exception and the error. However every method must explicity state the types of checked exceptions that it can throw. For example if you are reading/writing a file, your method can throw IOException:

public void ReadCSV throws IOException

We are telling the compiler that our ReadCSV method can throw IOException. If our method can throw multiple exceptions, we should mention that explicitly:

public void ReadCSV throws Exceptio1, Exception2, Exception3

Throwing Exceptions
When the compiler detects an error during program execution it creates an instance of appropriate exception class and throws it like

throws new IllegalArgumentException("Wrong Argument")

This happens when the method wants a positive number but it is passed a negative number.

Catching Exceptions
When an exception is thrown, we can catch it.

try {
statements;
}
catch (Exception1 ex) {
handler of Exception1:
}

If no exception is thrown by the try block, catch block is skipped execution.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)