xxxxxxxxxx
try:
#insert code here
except:
#insert code that will run if the above code runs into an error.
except ValueError:
#insert code that will run if the above code runs into a specific error.
#(For example, a ValueError)
xxxxxxxxxx
try:
# Dangerous stuff
except ValueError:
# If you use try, at least 1 except block is mandatory!
# Handle it somehow / ignore
except (BadThingError, HorrbileThingError) as e:
# Hande it differently
except:
# This will catch every exception.
else:
# Else block is not mandatory.
# Dangerous stuff ended with no exception
finally:
# Finally block is not mandatory.
# This will ALWAYS happen after the above blocks.
xxxxxxxxxx
try:
print(x)
except SyntaxError:
print("There is a SyntaxError in your code")
except NameError:
print("There is a NameError in your code")
except TypeError:
print("There is a TypeError in your code")
xxxxxxxxxx
>>> def divide(x, y):
try:
result = x / y
except ZeroDivisionError:
print("division by zero!")
else:
print("result is", result)
finally:
print("executing finally clause")
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'
xxxxxxxxxx
x = 5
y = "hello"
try:
z = x + y
except TypeError:
print("Error: cannot add an int and a str")
xxxxxxxxxx
In Java, you can access a variable from another class using public access modifier or getter methods.
Example 1: Using public Modifier
class A {
public int num = 10;
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.num); // Accessing public variable
}
}
Example 2: Using Getter Method
class A {
private int num = 10;
public int getNum() { // Getter method
return num;
}
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.getNum()); // Accessing via getter
}
}
Using getter methods is recommended for encapsulation.
xxxxxxxxxx
In Java, you can access a variable from another class using public access modifier or getter methods.
Example 1: Using public Modifier
class A {
public int num = 10;
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.num); // Accessing public variable
}
}
Example 2: Using Getter Method
class A {
private int num = 10;
public int getNum() { // Getter method
return num;
}
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.getNum()); // Accessing via getter
}
}
Using getter methods is recommended for encapsulation.
xxxxxxxxxx
In Java, you can access a variable from another class using public access modifier or getter methods.
Example 1: Using public Modifier
class A {
public int num = 10;
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.num); // Accessing public variable
}
}
Example 2: Using Getter Method
class A {
private int num = 10;
public int getNum() { // Getter method
return num;
}
}
class B {
public static void main(String[] args) {
A obj = new A();
System.out.println(obj.getNum()); // Accessing via getter
}
}
Using getter methods is recommended for encapsulation.