Java Exception Handling Tutorials
In this detailed Resource page, we feature an abundance of AWT Tutorials!
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs, computer hardware mechanisms like interrupts or operating system IPC facilities like signals.
In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler. The details of how this is done depends on whether it is a hardware or software exception and how the software exception is implemented. Some exceptions, especially hardware ones, may be handled so gracefully that execution can resume where it was interrupted.
Alternative approaches to exception handling in software are error checking, which maintains normal program flow with later explicit checks for contingencies reported using special return values or some auxiliary global variable such as C’s errno or floating point status flags; or input validation to preemptively filter exceptional cases.
Software exception handling and the support provided by software tools differs somewhat from what is understood by exception handling in hardware, but similar concepts are involved. In programming language mechanisms for exception handling, the term exception is typically used in a specific sense to denote a data structure storing information about an exceptional condition. One mechanism to transfer control, or raise an exception, is known as a throw. The exception is said to be thrown. Execution is transferred to a “catch”.
From the point of view of the author of a routine, raising an exception is a useful way to signal that a routine could not execute normally – for example, when an input argument is invalid (e.g. value is outside of the domain of a function) or when a resource it relies on is unavailable (like a missing file, a hard disk error, or out-of-memory errors). In systems without exceptions, routines would need to return some special error code. However, this is sometimes complicated by the semipredicate problem, in which users of the routine need to write extra code to distinguish normal return values from erroneous ones.
Programming languages differ substantially in their notion of what an exception is. Contemporary languages can roughly be divided into two groups:
- Languages where exceptions are designed to be used as flow control structures: Ada, Java, Modula-3, ML, OCaml, Python, and Ruby fall in this category.
- Languages where exceptions are only used to handle abnormal, unpredictable, erroneous situations: C++, C#, Common Lisp, Eiffel, and Modula-2.
Kiniry also notes that “Language design only partially influences the use of exceptions, and consequently, the manner in which one handles partial and total failures during system execution. The other major influence is examples of use, typically in core libraries and code examples in technical books, magazine articles, and online discussion forums, and in an organization’s code standards.”
Contemporary applications face many design challenges when considering exception handling strategies. Particularly in modern enterprise level applications, exceptions must often cross process boundaries and machine boundaries. Part of designing a solid exception handling strategy is recognizing when a process has failed to the point where it cannot be economically handled by the software portion of the process.
Java Exception Handling Tutorials – Getting Started
Simple examples based on Java Exception Handling
- java.lang.unsupportedclassversionerror – How to handle Unsupported Class Version Error
In this tutorial we will discuss about Java’s UnsupportedClassVersionError and how to deal with it. The UnsupportedClassVersionError is a sub-class of the LinkageError class and specifically, of the ClassFormatError class. - java.lang.nosuchmethoderror – How to avoid
In this tutorial we will discuss about Java’s NoSuchMethodError and how to deal with it. The NoSuchMethodError is a sub-class of the LinkageError class and denotes that an application code is trying to call a specified method of a class, either static or instance, and that class has no longer a definition for that method. - java.lang.NoClassDefFoundError – How to solve No Class Def Found Error
In this tutorial we will discuss How to solve No Class Def Found Error (NoClassDefFoundError). This error is thrown when the Java Virtual Machine (JVM) or an instance of the ClassLoader class tries to load the definition of a class, but the definition could not be found. - java.lang.StackOverflowError – How to solve StackOverflowError
In this tutorial we will discuss about the StackOverflowError in Java. This error is thrown to indicate that the application’s stack was exhausted, due to deep recursion. - java.lang.AbstractMethodError – How to resolve Abstract Method Error
In this example we will discuss about AbstractMethodError. As you may have figured out, this is thrown when the application calls an abstract method. - java.lang.arrayindexoutofboundsexception – How to handle Array Index Out Of Bounds Exception
Java supports the creation and manipulation of arrays, as a data structure. A bunch of Java data structures are implemented using arrays to store and expand their data. Thus, arrays are massively used in programming, as they provide a very fast and easy way to store and access pieces of data. - java.lang.NullPointerException – How to handle Null Pointer Exception
In Java, a special null value can be assigned to an object’s reference and denotes that the object is currently pointing to unknown piece of data. A NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null. - java.lang.unsatisfiedlinkerror – How to handle Unsatisfied Link Error
The UnsatisfiedLinkError is a sub-class of the LinkageError class and denotes that the Java Virtual Machine (JVM) cannot find an appropriate native-language definition of a method declared as native. This error exists since the first release of Java (1.0) and is thrown only at runtime. - java.lang.IllegalArgumentException – How to solve Illegal Argument Exception
This exception is thrown in order to indicate that a method has been passed an illegal or inappropriate argument. For example, if a method requires a non-empty string as a parameter and the input string equals to null, the IllegalArgumentException is thrown to indicate that the input parameter cannot be null. - java.io.FileNotFoundException – How to solve File Not Found Exception
This exception is thrown during a failed attempt to open the file denoted by a specified pathname. Also, this exception can be thrown when an application tries to open a file for writing, but the file is read only, or the permissions of the file do not allow the file to be read by any application. - java.io.ObjectStreamException – How to solve Object Stream Exception
This exception is defined as the superclass of all exceptions specific to Object Stream classes. The ObjectStreamException is defined as an abstract class and thus, an instance of ObjectStreamException cannot be created. - java.lang.ClassNotFoundException – How to solve Class Not Found Exception
In this tutorial we will discuss about ClassNotFoundException. This exception is thrown when an application tries to load a class through its string name, but no definition for the specified class name could be found. - java.io.IOException – How to solve IOException
In this example we are going to talk about a very common exception that many java developers stumble upon when dealing with IO operations in their program : IOException. This exception occurs when an IO operation has failed for some reason. - java.net.UnknownHostException – How to solve UnknownHostException
In this tutorial we are going to talk about java.net.UnknownHostException. This is a subclass of IOException, so it is a checked exception. It emerges when you are trying to connect to a remote host using its host name, but the IP address of that host cannot be resolved. - java.net.MalformedURLException – How to solve MalformedURLException
It is a subclass of IOException so it is a checked exception. What you should know is that MalformedURLException is an exception that occurs when you are trying to connect to a URL from your program but your client cannot parse the URL correctly. - java.lang.NumberFormatException – How to solve NumberFormatException
This is an unchecked exception and it can occur when you are trying to convert a String to a numeric value, like an Integer or a Float, but the String is not well formatted for the conversion. - java.net.SocketException – How to solve SocketException
In this example we are going to talk about java.net.SocketException. This is a subclass of IOException so it’s a checked exception. It is the most general exception that signals a problem when trying to open or access a socket. - java.net.SocketTimeoutException – How to Solve SocketTimeoutException
In this example we are going to talk about java.net.SocketTimeoutException. This exception is a subclass of java.io.IOException, so it is a checked exception. From the javadoc we read that this exception :” Signals that a timeout has occurred on a socket read or accept”. - java.io.NotSerializableException – How to solve Not Serializable Exception
In this tutorial we will discuss about NotSerializableException in Java. The exception is thrown when an instance of a class must implement the Serializable interface. - java.text.ParseException – How to Solve ParseException
In this example we are going to talk about java.text.ParseException. This is a checked exception an it can occur when you fail to parse a String that is ought to have a special format. One very significant example on that is when you are trying to parse a String to a Date Object. - java.rmi.RemoteException – How to solve RemoteException
In this example we are going to talk about java.rmi.RemoteException. This the most general checked exception that may occur during the lookup or the execution of a Remote Procedure Call (RPC). - java.util.NoSuchElementException – How to solve NoSuchElementException
In this tutorial we will discuss about NoSuchElementException in Java. This exception is thrown to indicate that there are no more elements in an enumeration. - java.lang.UnsupportedOperationException – How to handle UnsupportedOperationException
In this tutorial we will discuss about UnsupportedOperationException in Java. This exception is thrown to indicate that the requested operation is not supported. - java.lang.IllegalMonitorStateException – How to solve IllegalMonitorStateException
In this tutorial we will discuss about the IllegalMonitorStateException in Java. This exception, when thrown, indicates that the calling thread has attempted to wait on an object’s monitor, or has attempted to notify other threads that wait on an object’s monitor, without owning the specified monitor. - java.io.EOFException – How to solve EOFException
In this tutorial we will discuss about the EOFException in Java. This exception indicates the the end of file (EOF), or the end of stream has been reached unexpectedly. - java.util.concurrentmodificationexception – How to handle Concurrent Modification Exception
The ConcurrentModificationException is a RuntimeException that may be thrown by methods that have detected concurrent modification of an object, when such modification is not permissible. - java.util.concurrent.RejectedExecutionException – How to solve RejectedExecutionException
In this example we are going to talk about java.util.concurrent.RejectedExecutionException. When using an Executor to run your threads, it might reach a state where it cannot run the task you asked him to. - Java Custom Exception Example
In this example we will look briefly at the basics of Exception, in Java Programming Language. We will also see, how to create a custom Exception Class.
[undereg]