Skip to main content

Posts

Showing posts with the label exceptions

How does exception handling work in Java?

1.It separates the working/functional code from the error-handling code by way of try-catch clauses. 2.It allows a clean path for error propagation. If the called method encounters a situation it can't manage, it can throw an exception and let the calling method deal with it. 3.By enlisting the compiler to ensure that "exceptional" situations are anticipated and accounted for, it enforces powerful coding. 4.Exceptions are of two types: Compiler-enforced exceptions, or checked exceptions. Runtime exceptions, or unchecked exceptions. Compiler-enforced (checked) exceptions are instances of the Exception class or one of its subclasses — excluding the RuntimeException branch. 

Java Exceptions Interview Questions - Latest

Explain the user defined Exceptions? User defined Exceptions are custom Exception classes defined by the user for specific purpose. A user defined exception can be created by simply sub-classing an Exception class or a subclass of an Exception class. This allows custom exceptions to be generated (using throw clause) and caught in the same way as normal exceptions. Example: class CustomException extends Exception { } What classes of exceptions may be caught by a catch clause? A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types. Errors are generally irrecoverable conditions What is the difference between exception and error? Error's are irrecoverable exceptions. Usually a program terminates when an error is encountered. What is the difference between throw and throws keywords? The throw keyword denotes a statement that causes an exception to be initiated. It takes the Exception object to be thrown as an a...

Common Syntax Errors That You May Do in Java Programing

Capitalisation of key words Once we get into the habit of writing class names in capital letters you will occasionally find yourself writing keywords such as class and int with a capital beginning letter. The compiler will object to this and will issue an error message which depends on which keyword was capitalized. The compiler will issue an error message such as: Line nn: class or interface declaration expected when, for example, you capitalize the keyword class . Writing a string over a new line Sometimes you will need to write a long string. A common error is to have a new line embedded in the string. The compiler will object to this and will issue an error message such as: Line nn: ';' expected When this happens the solution is to split the string into two, making sure that neither string has a new line in it, and concatenate them with +. Thus you might replace: