xxxxxxxxxx
# If in the iterate has continu or break, finally must run.
for i in range(2, -3, -1):
try:
result = 4 / i
print(f'4/{i}={result}')
except ZeroDivisionError as ex:
print(ex)
print('even continue still run finally')
continue
finally:
print('finnaly must run!')
print('run loop again')
else:
print('while finish run here')
xxxxxxxxxx
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
xxxxxxxxxx
try:
#some code that may will produce an error
except:
#some code that will be executed if an error is encountered
finally:
#this code always will be executed
xxxxxxxxxx
'''
In python, you can use try, except and finally to catch errors to keep
running your code even when you run into an error.
try:
# insert code
except SpecificError:
# code that will run if the code in 'try' doesn't work
finally:
# always runs this code, error or not
'''
try:
myVar = 10 / 0 # runs into an error
except ZeroDivisionError as error:
print(error) # prints error to user
finally:
print('Finished try, except, finally') # always prints
xxxxxxxxxx
try:
run_code1()
except TypeError:
run_code2()
return None # The finally block is run before the method returns
finally:
other_code()
xxxxxxxxxx
try:
f = open("test.txt",encoding = 'utf-8')
# perform file operations
finally:
f.close()
xxxxxxxxxx
a = 10
try:
print(a + b)
except NameError:
print("NameError exception has been raised.")
else:
print("This will not be executed.")
finally:
print("This will always be executed.")
xxxxxxxxxx
import React, { useEffect, useRef } from 'react';
import L, { Map } from 'leaflet'; // Import Map type from Leaflet
import 'leaflet/dist/leaflet.css';
import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css';
import 'leaflet-defaulticon-compatibility';
import { LucideMapPin } from 'lucide-react';
type MapProps = {
property_address: string;
map_lat: number;
map_lng: number;
};
export default function MapComponent({ property_address, map_lat, map_lng }: MapProps) {
const mapRef = useRef<Map | null>(null); // Explicitly type mapRef
useEffect(() => {
if (!mapRef.current) {
// Initialize the map only if it hasn't been initialized yet
mapRef.current = L.map('map').setView([map_lat, map_lng], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(mapRef.current);
L.marker([map_lat, map_lng])
.addTo(mapRef.current)
// .bindPopup("A marker!")
.openPopup();
}
return () => {
// Cleanup function to remove the map instance
if (mapRef.current) {
mapRef.current.remove();
mapRef.current = null; // Reset the ref
}
};
}, []);
return (
<section>
<div id="map" className="rounded-lg" style={{ height: '500px', width: '100%' }} />
<div className="text-gray-500 max-xs:text-sm text-xl flex gap-2 items-end">
<LucideMapPin className="max-xs:w-5" />
<p>{property_address}</p>
</div>
</section>
);
}