Server Side Programming Articles - Page 1570 of 2650

PHP Exception Handling with finally

Malhar Lathkar
Updated on 18-Sep-2020 08:45:23

410 Views

IntroductionCode in finally block will always get executed whether there is an exception in ry block or not. This block appears either after catch block or instead of catch block.catch and finally blockIn following example, both catch and finally blocks are given. If execption occurs in try block, code in both is executed. If there is no exception, only finally block is executed.Example Live DemoOutputFollowing output is displayedCaught exception: Division by zero. This block is always executed Execution continueschange statement in try block so that no exception occursExample Live DemoOutputFollowing output is displayed2 This block is always executed Execution continuesfinally block onlyFollowing ... Read More

PHP Extending Exceptions

Malhar Lathkar
Updated on 18-Sep-2020 08:42:05

1K+ Views

IntroductionException class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __tostring() method that can be overridden to return a string representation of Exception object.final public function getMessage()message of exceptionfinal public function getCode()code of exceptionfinal public function getFile()source filenamefinal public function getLine()source linefinal public function getTrace()an array of the backtrace()final public function getPrevious()previous exceptionfinal public function getTraceAsString()formatted string of tracepublic function __toString()formatted string for displayIf user defined exception class re-defines the constructor, it should call parent::__construct() to ensure ... Read More

PHP declare Statement

Malhar Lathkar
Updated on 18-Sep-2020 12:52:13

666 Views

IntroductionSyntax of declare statement in PHP is similar to other flow control structures such as while, for, foreach etc.Syntaxdeclare (directive) {    statement1;    statement2;    . . }Behaviour of the block is defined by type of directive. Three types of directives can be provided in declare statement - ticks, encoding and strict_types directive.ticks directiveA tick is a name given to special event that occurs a certain number of statements in the script are executed. These statements are internal to PHP and roughky equal to the statements in your script (excluding conditional and argument expressions. Any function can be associated ... Read More

Retrieving Elements from Collection in Java- EnumerationIterator

AmitDiwan
Updated on 14-Sep-2020 09:41:53

232 Views

EnumerationIterator doesn’t have the option of eliminating elements from the collection, whereas an iterator has this facility. An additional disadvantage of using EnumerationIterator is that the name of methods associated with EnumerationIterator is difficult to remember.ExampleFollowing is an example − Live Demoimport java.util.Vector; import java.util.Enumeration; public class Demo {    public static void main(String args[]) {       Vector day_name = new Vector();       day_name.add("Tuesday");       day_name.add("Thursday");       day_name.add("Saturday");       day_name.add("Sunday");       Enumeration my_days = day_name.elements();       System.out.println("The values are ");       while (my_days.hasMoreElements())     ... Read More

Retrieving Elements from Collection in Java- ListIterator

AmitDiwan
Updated on 14-Sep-2020 09:39:05

237 Views

Following is an example to retrieve elements from Collection in Java-ListIterator −Example Live Demoimport java. util.* ; public class Demo {    public static void main(String args[]) {       Vector my_vect = new Vector();       my_vect.add(56);       my_vect.add(78);       my_vect.add(98);       my_vect.add(34);       ListIterator my_iter = my_vect.listIterator();       System.out.println("In forward direction:");       while (my_iter.hasNext())       System.out.print(my_iter.next()+" ") ;       System.out.print("In backward direction:") ;       while (my_iter.hasPrevious())          System.out.print(my_iter.previous()+" ");    } }OutputIn forward direction: 56 78 ... Read More

Retrieving Elements from Collection in Java- Iterator

AmitDiwan
Updated on 14-Sep-2020 09:36:16

368 Views

Following is an example to retrieve elements −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet my_hs = new HashSet() ;       my_hs.add("Joe");       my_hs.add ("Rob");       Iterator my_it = my_hs.iterator();       System.out.println("The elements are : ");       while (my_it.hasNext())          System.out.println(my_it.next());    } }OutputThe elements are : Joe RobA class named Demo contains the main function, which defines a HashSet collection. Elements are added to this collection using the ‘add’ function. An iterator is defined, and the ... Read More

Retrieving Elements from Collection in Java- For-each loop

AmitDiwan
Updated on 14-Sep-2020 09:34:04

326 Views

The ‘for-each’ loop is used to iterate over a set of elements that is stored in a data structure.Syntaxfor (element e: collection) {    System.out.println(e); }ExampleFollowing is an example − Live Demopublic class Demo {    public static void main(String[] args) {       int[] my_vals = {5, 67, 89, 31, -1, 2, 0};       int sum = 0;       for (int number: my_vals) {          sum += number;       }       System.out.println("The sum is " + sum);    } }OutputThe sum is 193A class named Demo contains the ... Read More

Reflection Array Class in Java

AmitDiwan
Updated on 14-Sep-2020 09:26:40

587 Views

The array class present in java.lang.reflect package belong to the Java reflection class. The Java Reflection class provides static methods, and these methods can be used to create and access Java arrays in a dynamic manner. This class is final, and this means it can’t be changed or even instantiated. The methods present inside this class can be used with the help of the class name itself.The methods present in java.util.Arrays.class can be used to work with arrays, and the java.lang.reflect.Array class contains methods that help in creating and working with Java arrays in a dynamic manner.The java.lang.reflect.Array class is ... Read More

Semaphore in Java

AmitDiwan
Updated on 14-Sep-2020 09:21:29

700 Views

A semaphore is used to control access to a shared resource when a process is being executed. This is done with the help of a counter. When this counter value is greater than 0, access to share resource is provided. On the other hand, if the value of counter is zero, then access to shared resources is denied. The counter basically keeps a count of the number of permissions it has given to the shared resource. This means, a semaphore provides access to a shared resource for a thread.Semaphores are present in the java.util.concurrent package. The concept of semaphore is ... Read More

Quantifiers in Java

AmitDiwan
Updated on 14-Sep-2020 09:19:31

310 Views

Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example − Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       Pattern my_pattern = Pattern.compile("sam+?");       Matcher my_match = my_pattern.matcher("samp");       while (my_match.find())       System.out.println("The pattern has been found - " + my_match.start() ... Read More

Advertisements