Tutorialspoint

Java Arithmetic Operators

public class Test {
   public static void main(String args[]) { 
      int a = 10; int b = 20; int c = 25; int d = 25; 
      System.out.println("a + b = " + (a + b) ); 
      System.out.println("a - b = " + (a - b) );
      System.out.println("a * b = " + (a * b) ); 
      System.out.println("b / a = " + (b / a) ); 
      System.out.println("b % a = " + (b % a) );
      System.out.println("c % a = " + (c % a) ); 
      System.out.println("a++ = " + (a++) ); 
      System.out.println("b-- = " + (a--) ); 
      
      // Check the difference in d++ and ++d 
      System.out.println("d++ = " + (d++) ); 
      System.out.println("++d = " + (++d) ); 
   } 
}

java.lang.Throwable.toString()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         Exception2();
      } catch(Throwable e) {
         System.err.println(e.toString());
      }
   }
  
   public static void Exception2()throws Throwable {
      throw new Throwable("This is the new Exception"); 
   }
} 

java.lang.Throwable.setStackTrace()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         ExceptionFunc();
      } catch(Throwable e) {
         // access to the stack trace
         StackTraceElement[] trace = e.getStackTrace();
         System.err.println(trace[0].toString());
      }
   }
  
   public static void ExceptionFunc()throws Throwable {

      Throwable t = new Throwable("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",10)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
}

java.lang.Throwable.printStackTrace(PrintWriter s)

package com.tutorialspoint;

import java.lang.*;
import java.io.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      Throwable t = new IllegalArgumentException("ABCD");
      System.out.println(getStackTrace(t));  
   }
 
   public static String getStackTrace(Throwable t) {
  
      /* prints this throwable and its backtrace to the specified
      print writer. */
      Writer wr = new StringWriter();
      PrintWriter pWriter = new PrintWriter(wr);
      t.printStackTrace(pWriter);
      return wr.toString();
   }
}

java.lang.Throwable.printStackTrace()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         ExceptionFunc();
      } catch(Throwable e) {
         // prints stacktace for this Throwable Object
         e.printStackTrace();
      }
   }
  
   public static void ExceptionFunc() throws Throwable {

      Throwable t = new Throwable("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",5)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
} 

java.lang.Throwable.initCause()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         Exception1();
      } catch(Exception e) {
         System.out.println(e);
      }
   }
  
   public static void Exception1()throws amitException{
      try {
         Exception2();
      } catch(otherException e) {
         amitException a1 = new amitException();
     
         // initializes the cause of this throwable to the specified value. 
         a1.initCause(e);
         throw a1;
      }
   }
  
   public static void Exception2() throws otherException {
      throw new otherException();
   }
}

class amitException extends Throwable {
   amitException() {
      super("This is my Exception....");
   }
}

class otherException extends Throwable {
   otherException() {
      super("This is any other Exception....");
   }
} 

java.lang.Throwable.getStackTrace()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         ExceptionFunc();
      } catch(Throwable e) {
         // access to the stack trace
         StackTraceElement[] trace = e.getStackTrace();
         System.err.println(trace[0].toString());
      }
   }
  
   public static void ExceptionFunc()throws Throwable {

      Throwable t = new Throwable("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",15)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
}

java.lang.Throwable.getMessage()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

     try {
         newException();
      } catch(Throwable e) { 
         System.err.println(e);
         // returns the detail message string of this Throwable instance 
         System.out.println(e.getMessage());
      }
   }
  
   public static void newException() throws Exception {
      System.out.println("This is newException() function");
      throw new Exception("new Exception...");
   }
} 

java.lang.Throwable.getLocalizedMessage()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

     try {
         newException();
      } catch(Throwable e) { 
         System.err.println(e);
         // localized description of this throwable
         System.out.println(e.getLocalizedMessage());
      }
   }
  
   public static void newException() throws Exception {
      System.out.println("This is newException() function");
      throw new Exception("new Exception...");
   }
} 

java.lang.Throwable.getCause()

package com.tutorialspoint;

import java.lang.*;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

      try {
         newException();
      } catch(Throwable e) {
         System.err.println(e);
         // returns null as the cause is nonexistent or unknown.
         System.err.println("Cause = " + e.getCause());
      }
   }
  
   public static void newException() throws Exception {
      System.out.println("This is newException() function");
      throw new Exception("Exception...");
   }
}

1 2 3 4 5 6 7 ... 396 Next
Advertisements
Loading...