You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 5, 2023. It is now read-only.
Exception is thrown by the compiler. But developers can manually throw an exception using throw.
//throwtry{}
catch(ArithemticExceptione){
}
catch(NullPointerExceptione){
}
//throw, But from JDK 7try{
}
catch(NullPointerException | ArithemticExceptione){
}
main()==>f2() rethrow
Don't use the throw keyword at the beginning as it makes the remaining parts of a program unreachable.
Throw (Exception)
Manually raise
Chained Exception
rethrow
User-defined
Chained exception helps to identify a situation in which one execution causes another exception.
Rethrow: If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.
Program 1: Using throw keyword to manually create an excpetion
classTest{
publicstaticvoidmain(Stringargs[]){
try{
// NullPointerException e=new NullPointerException("null");thrownewNullPointerException("null"); //Anonymous object to throw an exception.
}catch(NullPointerExceptione){
System.out.println("Caught"+e);
}
}
}
Output:-
[user]$ java Test
Caughtjava.lang.NullPointerException: null
[user]$ java Test
Test: Caught...java.lang.NullPointerException: null
mainjava.lang.NullPointerException: null
Exception in thread "main" java.lang.NullPointerException: null
at Test.test(Test.java:5)
at Test.main(Test.java:13)
[user]]$ java Test
test: Caught...java.lang.NullPointerException: null
Cause...java.lang.ArithmeticException
Exception in thread "main" java.lang.NullPointerException: null
at Test.main(Test.java:3)
Caused by: java.lang.ArithmeticException
at Test.main(Test.java:5)
class NegativeValueException-450.0 is negative
Account Created
Reference (Throwable class)
[user]$ javap java.lang.Throwable
Compiled from "Throwable.java"
public class java.lang.Throwable implements java.io.Serializable {
static final boolean $assertionsDisabled;
public java.lang.Throwable();
public java.lang.Throwable(java.lang.String);
public java.lang.Throwable(java.lang.String, java.lang.Throwable);
public java.lang.Throwable(java.lang.Throwable);
protected java.lang.Throwable(java.lang.String, java.lang.Throwable, boolean, boolean);
public java.lang.String getMessage();
public java.lang.String getLocalizedMessage();
public synchronized java.lang.Throwable getCause();
public synchronized java.lang.Throwable initCause(java.lang.Throwable);
final void setCause(java.lang.Throwable);
public java.lang.String toString();
public void printStackTrace();
public void printStackTrace(java.io.PrintStream);
public void printStackTrace(java.io.PrintWriter);
public synchronized java.lang.Throwable fillInStackTrace();
public java.lang.StackTraceElement[] getStackTrace();
public void setStackTrace(java.lang.StackTraceElement[]);
public final synchronized void addSuppressed(java.lang.Throwable);
public final synchronized java.lang.Throwable[] getSuppressed();
static {};
}