<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: trishareddy11</title>
    <description>The latest articles on DEV Community by trishareddy11 (@trishareddy11).</description>
    <link>https://dev.to/trishareddy11</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626652%2F5452a274-8d2a-406d-8cd8-b3cdfc368a78.png</url>
      <title>DEV Community: trishareddy11</title>
      <link>https://dev.to/trishareddy11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/trishareddy11"/>
    <language>en</language>
    <item>
      <title>User-defined Exceptions in Python</title>
      <dc:creator>trishareddy11</dc:creator>
      <pubDate>Thu, 06 May 2021 14:40:33 +0000</pubDate>
      <link>https://dev.to/trishareddy11/user-defined-exceptions-in-python-2ncd</link>
      <guid>https://dev.to/trishareddy11/user-defined-exceptions-in-python-2ncd</guid>
      <description>&lt;p&gt;In Python user-defined exceptions ,users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class.&lt;/p&gt;

&lt;h1&gt;
  
  
  CREATING A USER-DEFINED EXCEPTION CLASS
&lt;/h1&gt;

&lt;p&gt;Here we created a new exception class i.e. User_Error. Exceptions need to be derived from the built-in Exception class, either directly or indirectly. Let’s look at the given example which contains a constructor and display method within the given class.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#class MyError is extented from super class Exception&lt;br&gt;
class User_Error(Exception):&lt;br&gt;
  # constructor method&lt;br&gt;
  def __init__(self,value):&lt;br&gt;
    self.value = value&lt;br&gt;
  # __str__display function&lt;br&gt;
  def __str__(self):&lt;br&gt;
    return(repr(self.value))&lt;br&gt;
try:&lt;br&gt;
  raise(User_Error("User defined error"))&lt;br&gt;
  # Value of Exception is stored in error&lt;br&gt;
except User_Error as error:&lt;br&gt;
  print('A New Exception occured:',error.value)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  CREATING A USER-DEFINED EXCEPTION CLASS (USING MULTIPLE INHERITENCE)
&lt;/h1&gt;

&lt;p&gt;Derived class Exceptions are created when a single module handles multiple several distinct errors. Here we created a base class for exceptions defined by that module. This base class is inherited by various user-defined class to handle different types of errors.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# define python user-defined exceptions&lt;br&gt;
class Error(Exception):&lt;br&gt;
  """Base clase for other exceptions"""&lt;br&gt;
  pass&lt;br&gt;
class Dividebyzero(Error):&lt;br&gt;
  """Raised when the input value is zero"""&lt;br&gt;
  pass &lt;br&gt;
try:&lt;br&gt;
  i_num = int(input("Enter a number: "))&lt;br&gt;
  if i_num ==0:&lt;br&gt;
    raise Dividebyzero&lt;br&gt;
except Dividebyzero:&lt;br&gt;
  print("Input value is zero, try again!")&lt;br&gt;
  print()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Creating a User-defined Exception class (standard Exceptions)
&lt;/h1&gt;

&lt;p&gt;Runtime error is a built-in class which is raised whenever a generated error does not fall into mentioned categories. The program below explains how to use runtime error as base class and user-defined error as derived class.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# User defined error&lt;br&gt;
class Usererror(RuntimeError):&lt;br&gt;
   def __init__(self,arg):&lt;br&gt;
      self.args = arg&lt;br&gt;
try:&lt;br&gt;
   raise Usererror("usererror")&lt;br&gt;
except Usererror as e:&lt;br&gt;
   print (e.args)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
